[Stackless] hi all, can this be implemented ?

Christian Tismer tismer at stackless.com
Sun Oct 3 20:27:39 CEST 2004


zouguangxian wrote:

> hi all,
>    i am a newbie in stackless python. but i am attracted
> by it .now i want to embed stackless python in my c++
> program .i hope it can work in this way :
>    there is a "event" list,some thread will put  "event"
> in it. and the script engine thread will pick "event"
> from this list. pseudocode may like this :
> 
> python_generator generator ;   // to save generator object,can i do this ?
> 
> while (1) {
>    event=pickevent();
>    if (event==EVENT1) {
>        python.call(func1)
>    } else (if event==EVENT2) {
>        generator.resum();
>        python.call(func2);   
>    }
> }
> 
> ...
> void func1()
> {
>    ....
>    generator.suspend; //point 1
>    ...
> }
> 
> 
>   yes,when i receive EVENT1,the func1 was called, and
>   after some statement,i hope it will be paused at point
>   1, but will block the main loop. when EVENT2 was
>   received in main loop, then resume the func1,let it
>   continue run from point 1. can i do in this way ? 
>   maybe i should explain more . i want to embed stackless
>   python in a game as scripting engine. maybe NPC will
>   talk to player and wait the player to click on some
>   button , I hope the tasklet (i think it is ,can i think
>   so ?) will be  paused before the player click the button
>   and will not block main loop. once the player click on
>   some button , the tasklet will continue at the paused point .
>   can you give me some advice ? do i need callback
>   instead of tasklet(micro-thread) ?

Something like this will surely be possible, and
probably simpler than you think.
Before trying to embed Stackless, I'd propose to
play with it interactively and to figure out how
tasklets and channels operate together. A few
examples are in the Wiki pages.

Pausing a tasklet can be done by either simply not
running it (removing it from the executables list),
or by letting it block on a channel. The latter
is the preferred way.

A simple example (untested):

def button_handler(ch):
    # do some setup
    while 1:  # compute forever
        msg = ch.receive()  # here we block until we get an event
        # maybe dispatch on msg
        # or simply do your action

ch = channel()
t = tasklet(button_handler)(ch)

# and now, somewhere in your GUI code

    def button1Click(self, event):
        ch.send("doit")

cheers - chris
-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
tismerysoft GmbH             :     Have a break! Take a ride on Python's
Carmerstr. 2                 :    *Starship* http://starship.python.net/
10623 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 31 86 04 18  home +49 30 802 86 56  mobile +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
     whom do you want to sponsor today?   http://www.stackless.com/

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



More information about the Stackless mailing list