PyPy implements Stackless‘s cooperative concurrency model. It uses a channel construct to synchronize hand-off of data between a sender and receiver. Unfortunately, it most cases it would be preferable to have a queued channel that supports asynchronous message passing between tasklets. So here’s a quick hack to make it work. I don’t think this will work if one uses preemptive scheduling.
class Qchannel(stackless.channel): def __init__(self, maxsize=0): stackless.channel.__init__(self) self.data = deque() def send(self, msg): self.data.append(msg) if self.balance < 0: stackless.channel.send(self,True) stackless.schedule() def receive(self): while len(self.data) == 0: stackless.channel.receive(self) v = self.data.popleft() stackless.schedule() return v