by otabdeveloper4 · 4y ago · 0 comments
The only advantage of green threading is the capability to do problem-specific optimized scheduling.
If you're not doing this then you don't need green threads.
The kernel might allocate that much, but it's just virtual memory.
The million threads risk slowing each other down with context switches, preventing any from finishing in time. An async event loop will focus on one connection at a time until `await`, and at least finish the tasks it attempts.
I haven't been in this situation, but async seems to have real advantages here. With OS threads, you don't control scheduling without costly synchronisation primitives.
You're assuming that an async event context switch is somehow vastly less costly than an OS context switch, which isn't true in the general case. (And unless you really went out of your way to make it happen then yours is the general case.)
Threads have separate stacks. Surely this costs at least a little, and adds up with the number of threads?
They are, and this is the entire premise green threads are built on. An OS context switch has to do a lot more including switching page tables and possibly performing a TLB flush.
(I do agree that the majority of python projects will get by just fine with regular threads though)
Isn’t the main advantage simply that you avoid making an absurd number of costly system calls?