Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Task(object):
def run(self):
while True:
try:
result = self.target.send(self.sendval)
if isinstance(result, SystemCall):
return result
# sub-routine call
if isinstance(result, types.GeneratorType):
# put the caller on the stack
self.stack.append(self.target)
self.sendval = None
self.target = result
else:
if not self.stack:
return
self.sendval = result
# resume execution to caller
self.target = self.stack.pop()
# current coroutine finished
except StopIteration:
# invalid state
if not self.stack:
raise
self.sendval = None
self.target = self.stack.pop()
# Example coroutine and sub-coroutine
def Accept(sock):
yield ReadWait(sock)
yield sock.accept()
def server(port):
# ...
while True:
# coroutine that calls another
client, addr = yield Accept(sock)
yield NewTask(handle_client(client, addr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.