3 ms·
Twisted (a python asynchronous network programming framework) does something similar, but inverted. Twisted functions return a Deferred object without blocking.
by tomn 16y ago
Twisted (a python asynchronous network programming framework) does something similar, but inverted. Twisted functions return a Deferred object without blocking. A Deferred is a promise of a value, so rather than passing the callback into the function, you register a callback with the deferred, and the function promises to call it later.
This type of thing has a few nice properties -- you can easily register more than one callback, as well as using them as a pipeline (kind-of like monads in haskell), which are awkward to implement with callbacks.
As an aside, in python you can trick generators into turning synchronous-looking code into asynchronous, which can actually make things look a lot neater if done properly -- i'll provide code if anyone's interested.
- viraptor 16y ago> python you can trick generators into turning synchronous-looking code into asynchronous Example: http://dieselweb.org/lib/ http://dieselweb.org/lib/ or https://github.com/saucelabs/monocle https://github.com/saucelabs/monocle Although Diesel2 is much more interesting, with greenlets support. No more messing about with `yield`, since the blocking functions themselves force a context switch. https://github.com/jamwt/diesel https://github.com/jamwt/diesel
- tomn 16y agoThat looks really interesting, though it seems a bit dangerous -- greenlet feels like a bit of a hack. I wish I'd found diesel before twisted; it seems a lot more elegant if you don't need any of the built-in protocols. The twisted model is good, there's just too much of it.