MiniAxon - Standing on Shed Skin - first steps towards a C++ Compiled Kamaelia working

February 21, 2007 at 02:20 PM | categories: python, oldblog | View Comments

Well, I looked at shedskin as I mentioned the other day, and now have this working locally. One thing we encourage people new to Kamaelia is to build their own mini-core of the system - Axon. This provides basic microprocesses, basic component communications and scheduling. Whilst this may sound hairy, having run the mini-axon tutorial past many new programmers, including the tutor list, it seems pretty OK. (The tutorial is pretty gentle and provides very small simple steps aimed at someone new to python) The rest of this post after the fold has the meat in.

The other handy thing hough is that this provides a neat mechanism for handling how to experiment with new systems written from scratch, such as Shed Skin. As a result, I've done this, and the first step towards a Kamaelia system is the ability to tag a generator with context, and run multiples of them. Along the way I've discovered many limitations of shed skin (which is spectacular software IMO), which is by no means a criticism, but to its credit this can be worked around. As a result here's a basic concurrency framework in python that compiles directly to very reable C++.

Limitation 1, shed skin generators can't be a method. Therefore any class related stuff must not have any generator in it! Thus our microthread class looks like this:
  • class microthread:
        def __init__(self,name="hello"):
            self.name = name
The name is a test to check differentiation is happening.

Our microthread now is then just a standalone generator, but takes a mutable argument as if its pretending to be in the class:
  • def mainG(zelf):
        i = 1
        while i < 10:
           yield zelf.name + ":" + str(i)
           i = i+1
Incidentally, the way you therefore create a mainG microthread is as follows:
  • mainG(microthread("name")
This looks a little ugly, but the important thing as far as I'm concerned is, it works. Spectacular :)

Limitation 2: Shedskin looks at an argument "self", and thinks "that's bogus in a class definition, I'll get rid of that". This causes problems here, which is why we have "zelf" instead.

Limitation 3: No list comprehensions, and also if you put too many function calls together like(this(sort(of(thing())))), then shed skin creates intermediary classses to help itself. As a result you need to be MUCH more explicit about things you want to happen if you want to avoid this foibles.

So we create our microthreads:
  • N = [ "One", "Two", "Three", "Four" ]
    MT = []
    for n in N:
        MT.append( microthread(n) )

Attach the generator behaviour to them and put them in a simple process list:
  • PS = []
    for M in MT:
        U = mainG(M)
        PS.append(U)
And finally we can have a simple manual scheduler to run them:
  • while 1:
        for P in PS:
            beta = P.next()
            print 'hello, world!', beta
That is then translated to C++ by shedskin which you can then compile and run and get the following output:
  • hello, world! One:1
    hello, world! Two:1
    hello, world! Three:1
    hello, world! Four:1
    hello, world! One:2
    hello, world! Two:2
    hello, world! Three:2
    hello, world! Four:2
    hello, world! One:3
    hello, world! Two:3
    hello, world! Three:3
    hello, world! Four:3
    hello, world! One:4
    hello, world! Two:4
    hello, world! Three:4
    hello, world! Four:4
    hello, world! One:5
    hello, world! Two:5
    hello, world! Three:5
    hello, world! Four:5
    hello, world! One:6
    hello, world! Two:6
    hello, world! Three:6
    hello, world! Four:6
    hello, world! One:7
    hello, world! Two:7
    hello, world! Three:7
    hello, world! Four:7
    hello, world! One:8
    hello, world! Two:8
    hello, world! Three:8
    hello, world! Four:8
    hello, world! One:9
    hello, world! Two:9
    hello, world! Three:9
    hello, world! Four:9
    terminate called after throwing an instance of '__shedskin__::StopIteration*'
    Aborted
This is exactly what I would expect, and is as far as I'm concerned absolutely spectacular on the part of Mark. Really, really cool work.

Now I'm having lunch, after lunch I'll see if I can get the rest of a mini-axon up and running. If so, this could be quite quite fun :-)

blog comments powered by Disqus