The conversation

Business & incomeExperience excerpt · English

One of the things I had to wrap my head around was the whole async server style of programming

One of the things I had to wrap my head around was the whole async server style of programming. I learned that doing a sleep on the server consumes essentially zero resources. The thread goes to sleep and wakes up when the timer expires. In the meantime the server can handle other requests unhindered. You can have hundreds or thousands of these threads at the same time - serving requests, waiting for IO, etc. It’s not like synchronous servers e.g. Flask with Gunicorn (in the Python world) where you have a few pre-forked server processes. In that case it would be a waste to prevent one of those processes from serving other requests while sleeping, I agree. Ultimately it’s cool though to be able to control the client from the backend asynchronously, updating signals (think of them as a kind of special front-end variable with superpowers) when something on the backend happens, or updating the client’s DOM, or running scripts.

Original discussionHtmx – The Fetch()ening153 source comments · at import
2

What was this responding to?

Parent context · original excerpt

NP

Datastar looks interesting, but their guide on how to redirect a user is a joke. async def redirect_from_backend(): yield SSE.patch_elements('<div id="indicator">Redirecting in 3 seconds...</div>') await asyncio.sleep(3) yield SSE.execute_script('window.location = "/guide"') LOL, they really think it's a good use of server resources to sleep so the client doesn't have to. [1] https://data-star.dev/how_tos/redirect_the_page_from_the_bac...

Full original

Community replies 0

No community replies yet. Add your perspective.

Replies from the source 2

Direct replies available in this import. Original-language excerpts.

NP

To a point, but sleeping before redirecting on the server is insanity. It's pointlessly brittle when the client is perfectly capable of managing its own timer. This approach completely breaks sensible reasoning about what clients are responsible for. What's next, sending every keystroke to the server so it can tell the client to update a text input?!? It still takes resources to keep SSEs open, load balance, reopen lost connections, etc. But worst of all is shifting fundamental logic back to the server for literally no benefit.

Full original
NA

Sleeping on the server still consumes resources like memory, open sockets, etc, which may in turn cause some unnecessary CPU usage because of e.g. GC costs, scheduling costs, etc.

Full original
Continue on Hacker News

Respect different perspectives. Share concrete experiences.

Sign in to post, reply, repost, and quote.