views:

396

answers:

3
+3  A: 

You may want to look at Python Twisted. It is a nice Reactor based API that supports asynchronous operations. Proactor is the common term for asynchronous completion handler like frameworks.

grepsedawk
I have looked at Twisted some. It looks nice, but I don't think it would help me in structuring asynchronous operations that take place on the local machine.
Matt Green
@Matt sure it does. Not everything has to be network related. Understanding twisted's reactor pattern implementation will definitely help you, though.
Dustin
+2  A: 

Also have a look at the Asynchronous Completion Token and ActiveObject patterns.

Charlie Martin
+1  A: 

This sounds like the Observer design pattern. link.

Your client object is an Observer. Your API belongs to an object that's Observable.

Each client (in Java parlance) implements the Observer interface. In Python, it's a matter of each client offering a number of methods that your observable will use.

class SomeClientInterface( object ):
    def update( self, source, data ):
        # handle data being pushed from Observable source
    def error( self, from, status ):
        # handle error in Observable source

Your Observable object has a way for Observers to register and do other things.

class Observable( object ):
    def __init__( self ):
        self.clients= set()
    def register( self, observer ):
        self.clients.add( observer )
    def whenSomethingHappens( self ):
        # doing work
        if itAllWentToHell:
            for c in self.clients:
                c.error( self, "some status object" )
        else:
            for c in self.clients:
                c.update( self, the pushed data )
    def waitFor( self ):
        # observers are waiting...
        return theData
    def status( self ):
        return self.currentState
S.Lott