[Stackless] Dice1 embedded exemple

Richard richard at ccpgames.com
Mon Mar 20 09:53:39 CET 2006


> On Behalf Of Johan Carlsson
> 
> I'm trying to compile the Stackless\embedemo\dice1
> but it seems to use an old macro (can't find it in the source):
> PyTasklet_Call
> 
> Is there an easy fix to get the exampel running, or is there an
> better example?

Given the arguments PyTasklet_Call takes, PyTasklet_Setup seems
to be a renamed version of it (and a much better name too :)).

I have checked fixes for the code.

> The reason it attracted my attention was the claim to implement
> a sleep function which didn't consume any computation power
> while waiting.
> Maybe there is an better way to do that?

Well, if you look at the dice1 example, you can see that the C
function manually loops waiting for the time to pass.  So, it
does not meet that claim.

Take a look at the slides Kristján from CCP did for his
presentation at PyCon.  How we do this internally is covered
within them.  Although, without the accompanying presentation
to go with it, and with the example code being C++, it may be
a bit hard to discern exactly what needs to be done.

Clearer perhaps, is how I did it in Python in this old example
file of mine (do not look at the code more than you have to
as it is a bit of a mess being an old cropped version of some
personal code).  You should be able to write a C version
if that is what you want.  If anyone does this and wants to
contribute it as another Stackless demo file, I am sure it
would fit in nicely :)

http://www.stackless.com/Members/rmtew/StacklessMud

Specifically:

    ...

    def SleepUntil(self, time):
        channel = stackless.channel()
        self.sleepingTasklets.append([ time, channel ])
        self.sleepingTasklets.sort()
        return channel.receive()

    ...

    def CheckSleepingTasklets(self):
        currentTime = time.time()
        while len(self.sleepingTasklets):
            ETA, channel = self.sleepingTasklets[0]
            if ETA > currentTime:
                return
            del self.sleepingTasklets[0]
            stackless.tasklet(channel.send)(True)

And then the main tasklet loop does something like this:

    def Pump(self):
        while self.ok:
            t = stackless.run(1000)
            if t:
                t.insert()
            # Awaken anything that is due to be awoken.
            self.CheckSleepingTasklets()
            # Stop the Python process from hogging CPU.
            time.sleep(self.pumpSleepSeconds)

Hope this helps,
Richard.


_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless



More information about the Stackless mailing list