Home > Programming > Stackless

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

  1. January 13th, 2009 at 08:46 | #1

    Great start Tony,
    I like your style. It is easy to read and sophisticated at the same time. Look forward to read your next posts.

    Dimitre

  2. January 13th, 2009 at 12:49 | #2

    Thanks, I’m glad to see someone actually read it!

  3. Jennifer
    January 13th, 2009 at 13:01 | #3

    What a great way to express your thoughts and knowledge of the subject. :) The organization is great and I like the color scheme you have chosen. I hope you get the answers you are looking for to your question…

  4. January 13th, 2009 at 13:37 | #4

    Thanks for your support. I’m hope this sort of technical post doesn’t put you off of reading my posts :) . I still haven’t fully decided on a topic, but I’ll just write about whatever comes to mind for now, and hopefully I’ll settle on one area eventually. It’s so much fun! When are you starting your blog? :P

  5. Ashok
    January 26th, 2009 at 00:22 | #5

    Wow, I read that post twice and I’m so confused. Sounds cool though. I never thought about concurrent programming.
    Nice blog too, you might inspire me, haha.

  6. Tony
    January 26th, 2009 at 12:27 | #6

    Haha, I hope I do. We can be blogging buddies and leave each other trackbacks and pingbacks galore. Thanks for the comment. Now go do it!

  1. February 21st, 2009 at 05:14 | #1