def run(self, response=NONE): if self.finished: return try: # print self, "send", response if response is NONE: instruction = self.gen.next() else: instruction = self.gen.send(response) # Normal termination except StopIteration: self._done(None) return # Exceptional termination except: # TODO: Should we put the exception into the channel instead? self._done(None) raise if isinstance(instruction, Instruction): # Early termination if instruction.op == "stop": self._done(instruction.data) return if instruction.op == "put": channel, value = instruction.data put_then_callback(channel, value, self._continue) return # TODO: Should we throw if the value is an exception? if instruction.op == "take": channel = instruction.data take_then_callback(channel, self._continue) return # TODO: Timeout channel instead? if instruction.op == "sleep": seconds = instruction.data callback = lambda: self._continue(None) dispatch.queue_delay(callback, seconds) return if instruction.op == "alts": data = instruction.data operations = data.operations do_alts(operations, self._continue, priority=data.priority, default=data.default) return else: self._continue(instruction)
def take(channel): d = Deferred() take_then_callback(channel, d.callback) return d