3 ms·
> The point isn't the lines that contain "yield from". It's the lines that don't contain "yield from" What is the point though? Why does it matter where contex
by rdtsc 12y ago
> The point isn't the lines that contain "yield from". It's the lines that don't contain "yield from"
What is the point though? Why does it matter where context switches happen (either CPU or IO ones). You are doing the job of the scheduler which is like being teleported back to Windows 3.1. Adding "yield froms" turns your functions into generators. The job of code maybe is to update shopping carts or send tweets or something like that, now that function returns a generator and anything on top of it has to deal with it.
The original point, which I think you missed, is that the typical advantage of "yield from" (or deferreds, or callbacks) is that you don't need to worry about synchronization. Look no mutexes, this is fantastic! Except that is wrong. As the application grows, top level code starts to look like:
r1 = yield from f1()
r2 = yield from f2(r1)
r3 = yield from f3(r2)
...
With callbacks it looks even uglier. That code is logically approaching the code that looks like:
r1 = f1()
r2 = f2(r1)
r3 = f3(r2)
...
In a multithreaded program without mutexes.
Therefore this thing exists: http://twistedmatrix.com/documents/8.1.0/api/twisted.internet.defer.DeferredSemaphore.html http://twistedmatrix.com/documents/8.1.0/api/twisted.interne... and I had to use it often enough. Because what happens is two clients would start 2 concurrent callback chains, and if they start updating some shared data ( a database or internal structure ) you've got a data race and you need to use the DeferredSemaphore.
- lmm 12y ago> You are doing the job of the scheduler which is like being teleported back to Windows 3.1. Indeed it is. The difference is that modern programming techniques are good enough that we can do this with minimal overhead. Like how modern fighter planes have gone back to being aerodynamically unstable. > Therefore this thing exists: http://twistedmatrix.com/documents/8.1.0/api/twisted.interne.. http://twistedmatrix.com/documents/8.1.0/api/twisted.interne.... and I had to use it often enough. Because what happens is two clients would start 2 concurrent callback chains, and if they start updating some shared data ( a database or internal structure ) you've got a data race and you need to use the DeferredSemaphore. That's one approach, but as you say it simply recapitulates the problems of traditional multithreading. If that were the only option, we might as well use threads and mutices. But there are other options. We can accumulate effects that need to happen as a single transaction through our async chain (state monad) and then execute them all at once, with no possibility of yielding in the middle. In languages with true concurrency we can use an actor; in single-threaded event-driven interpreters we don't even need that. We get a model that has the power of open-and-close transactions (whether they be database transactions or or mutices), but is clearer and simpler to reason about.