Stackless
I’ve been reading about Stackless Python for concurrent programming and it sounds flippin’ fantastic. The most famous application, Eve Online, is very impressive.
There’s a really great tutorial on Stackless Python for those new to concurrent programming but familiar with Python written by Grant Olson. I did find one issue in the digital circuit code example, though.
I tried this with Python 2.5.1 and Python 2.6 to no avail. When I tried half-adder example, the output is incorrect.
A simple inputA(1) should give the output:
Result = 1 Carry = 0
Instead, it gives just:
Carry = 0
Or with debug mode on:
Setting input to 1 AndGate received 1 from <__main__.Switch instance at 0x012A5968> AndGate's new state => 0 Carry = 0
I modified the EventHandler class’ listen method to the following:
def listen(self):
while 1:
val = self.channel.receive()
self.processMessage(val)
if isinstance(self,AndGate):
debugPrint("AndGate outputs: %s" % self.outputs)
for output in self.outputs:
if isinstance(self,AndGate):
debugPrint("AndGate is outputing to: %s" % output)
self.notify(output)
And this is the result:
Setting input to 1 AndGate received 1 from <__main__.Switch instance at 0x012A5968> AndGate's new state => 0 AndGate outputs: [<__main__.Reporter instance at 0x012A5DC8>, <__main__.Inverter instance at 0x012A5EB8>] AndGate is outputing to: <__main__.Reporter instance at 0x012A5DC8> Carry = 0
As you can see, even though the And gate has 2 outputs (a Reporter and an Inverter), it’s only sending a message to the Reporter. Where did the message to the Inverter go?! Oddly enough, adding this to __main__ resolves the problem:
inverter((andGateA, andGateA.state)) inverter((andGateA, andGateA.state))
The output now (debug off) is:
Carry = 0 Result = 0 Result = 1 Result = 1 Result = 1
Now the final values it settles on (carry = 0, result = 1) are correct. It also works in the 3 other possible cases. So, for some reason the output from andGateA is not being sent (or received) properly by the Inverter. Any ideas? I’m stumped as to why. Could it be a bug in stackless? It is still in alpha…