Stackless

January 12th, 2009

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…

admin Programming

Welcome!

December 31st, 2008

This is the first entry of a what I hope will be a long and productive blogging career. In anything I do (and I think for most people), taking the first step is always the hardest part, so here it is. The first step. No more endless planning and pondering. Action!

I’ll keep it short because I feel like I should be working on my iPhone application project. Unfortunately, I can’t talk about in detail because of an NDA. I need to sort out with Michael (my “employer”) exactly what that covers, but I will be sharing general iPhone development perls of wisdom I gather along the way. The design is very temporary and will change drastically, but like I said I just wanted to get something out there to get the ball rolling. I want to thank my friends Yu-kai and Jun for inspiring me to join the social media community.

Welcome readers!

admin Programming