Re:Some thoughts on asynchronous Python API design in a post...
by marcosdumay on Monday November 07, 2016 @08:37AM
(#12890871)
> Whereas you have to lock every piece of state that you have.
You need to lock every piece of shared state you have. Where "shared" means stuff that many threads must communicate among themselves. One tends to keep the number of that kind of state low, really low. When zero is not possible, the most common number by a wide margin is one¹.
If you have more than 1, they are normally completely independent pieces of state that will not be used at the same time. If you have more than 1, and they are not independent, the code is either the result of at least one PHD thesis, or it does not work (or, often, both).
I bet you do network requests more than once on your code.
1 - The size of the shared state does not matter, so it's often one really big state.
Re:Some thoughts on asynchronous Python API design in a post...
by IgorPartola on Monday November 07, 2016 @08:18AM
(#12890758)
Every piece of shared state. In the case of something like a web server, you only need one lock: when the connection is being accepted and handed off to a worker thread. How many reads and writes does your web server perform?
You get an equal and opposite problem: whenever you add one more lock, you invalidate your liveness assumptions.
> The article claims that yield-based programming is easier because the fewer the total number of yield points, the less mental state a programmer needs to maintain. I don't think this argument is correct: in lock-based programming, we need to keep _zero_ preemption points in mind, because we assume every instruction is a yield point. Instead of thinking about NY program interleavings, we think about how many locks we hold. I bet we have fewer locks than you have yields.
I'll take that bet. You really don't have to yield very often - only when making a network request, and perhaps not even for that in the case of a fast local network. Whereas you have to lock every piece of state that you have.