[Python-Dev] Is there another way to solve the continuation problem?

Skip Montanaro skip at mojam.com
Tue May 18 17:49:42 CEST 1999


Okay, from my feeble understanding of the problem it appears that
coroutines/continuations and threads are going to be problematic at best for 
Sam's needs.  Are there other "solutions"?  We know about state machines.
They have the problem that the number of states grows exponentially (?) as
the number of state variables increases.

Can exceptions be coerced into providing the necessary structure without
botching up the application too badly?  Seems that at some point where you
need to do some I/O, you could raise an exception whose second expression
contains the necessary state to get back to where you need to be once the
I/O is ready to go.  The controller that catches the exceptions would use
select or poll to prepare for the I/O then dispatch back to the handlers
using the information from exceptions.

class IOSetup:
    pass

class WaveHands:
    """maintains exception raise info and selects one to go to next"""
    def choose_one(r,w,e):
	pass

    def remember(info):
	pass

def controller(...):
    waiters = WaveHands()
    while 1:
	r, w, e = select([...], [...], [...])
	# using r,w,e, select a waiter to call
	func, place = waiters.choose_one(r,w,e)
	try:
	    func(place)
	except IOSetup, info:
	    waiters.remember(info)


def spam_func(place):
    if place == "spam":
	# whatever I/O we needed to do is ready to go
	bytes = read(some_fd)
	process(bytes)
	# need to read some more from some_fd. args are:
	#    function, target, fd category (r, w), selectable object, 
	raise IOSetup, (spam_func, "eggs" , "r", some_fd)

    elif place == "eggs":
	# that next chunk is ready - get it and proceed...

    elif yadda, yadda, yadda...


One thread, some craftiness needed to construct things.  Seems like it might
isolate some of the statefulness to smaller functional units than a pure
state machine.  Clearly not as clean as continuations would be.  Totally
bogus?  Totally inadequate?  Maybe Sam already does things this way?


Skip Montanaro	| Mojam: "Uniting the World of Music" http://www.mojam.com/
skip at mojam.com  | Musi-Cal: http://www.musi-cal.com/
518-372-5583




More information about the Python-Dev mailing list