Re: Co-routines

Discussion in 'Python' started by Clark C. Evans, Jul 17, 2003.

  1. On Thu, Jul 17, 2003 at 11:07:36PM +1000, wrote:
    | I have an application in which I want the users to be able to create python
    | functions:
    |
    | 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:
    |
    | 1-1
    | 2-2
    | 1-2
    | 2-2
    | 1-3
    | 2-3

    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

    coop = flow.Cooperate()
    def f1():
    print "1-1"
    yield coop
    print "1-2"
    yield coop
    print "1-3"

    def f2():
    print "2-1"
    yield coop
    print "2-2"
    yield coop
    print "2-3"

    d = flow.Deferred(flow.Merge(f1(),f2()))
    d.addCallback(lambda _: reactor.stop())
    reactor.run()

    I'm working on converting many 'data sources' such as PostreSQL
    and OpenLDAP so that their async interface works this way.
    See http://www.twistedmatrix.com/documents/howto/flow.html
    for more information on the flow module.

    Best,

    Clark

    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, Jul 17, 2003
    #1
    1. Advertising

  2. Clark C. Evans

    Brian Kelley Guest


    > 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).
    >


    Check out this article:

    http://www-106.ibm.com/developerworks/opensource/library/l-pythrd.html?dwzone=opensource

    on weightless threads, a simpler version of "Flow"

    Now, my question is, could an arbitrary function's bytecode be liberally
    sprinkled with "yeilds" at run-time to achieve this effect without
    having to alter the source code?
    Brian Kelley, Jul 17, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Rich

    Proper serial routines ?

    Andrew Rich, Oct 4, 2003, in forum: Perl
    Replies:
    1
    Views:
    439
    Al Tobey
    Oct 24, 2003
  2. Soenke
    Replies:
    0
    Views:
    549
    Soenke
    Dec 28, 2005
  3. Matthew Hood

    How to create global Funtions & Routines

    Matthew Hood, Jun 24, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,916
    Matthew Hood
    Jun 25, 2003
  4. Keith A. Rowe
    Replies:
    2
    Views:
    293
    Keith A. Rowe
    Feb 14, 2004
  5. tshad
    Replies:
    2
    Views:
    699
    tshad
    Jan 28, 2005
Loading...

Share This Page