On Thu, Jul 17, 2003 at 11:07:36PM +1000, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
| I have an application in which I want the users to be able to create python
| def f1():
| print "1-1"
| print "1-2"
| print "1-3"
| def f2():
| print "2-1"
| print "2-2"
| print "3-3"
| and when my application runs, I want to execute these functions in
| "lock-step", so that the output looks like:
There is a 'Flow' module in Twisted which uses generators to
accomplish this sort of thing. Basically, Twisted has a
queue of functions that are called, and by yielding "cooperate"
object, you re-schedule the given generator for execution
on this queue (allowing other code blocks to execute).
from __future__ import generators
from twisted.flow import flow
from twisted.internet import reactor
P.S. If you wished, you could probably use flow without Twisted.
You'd have to do three things: (a) create an event queue object, such
as twisted.internet.reactor, (b) provide a notification when something
scheduled is done, aka twisted.internet.defer, and (c) provide some
way to handle exceptions in an async manner, aka twisted.python.fail.
Although, once you have all of these things in place... why not just
use the rest of Twisted... *grin* In other words, it is easy in
theory to implement this correctly, but not that easy if you want
to have it work with sockets and properly handle exceptions...
So, by the time you do all of this, you will have re-invented the
core of Twisted... and then you won't have a built-in http server
nor a community to support you.
Clark C. Evans
> There is a 'Flow' module in Twisted which uses generators to
> accomplish this sort of thing. Basically, Twisted has a
> queue of functions that are called, and by yielding "cooperate"
> object, you re-schedule the given generator for execution
> on this queue (allowing other code blocks to execute).