[Stackless] Blocking

Jeff Senn senn at maya.com
Tue Apr 26 15:28:36 CEST 2005


On Apr 26, 2005, at 7:19 AM, Gijs Kunze wrote:
> As I understand it, unlike what happens with 'real' threads, tasklets
> don't switch when doing a blocking operation (i/o, time.sleep), so in a
> tasklet you can only do those types of tasks asynchronously unless you
> want all tasklets to stop while completing said task. (please correct 
> me
> if I'm wrong). At first I thought you would have to make your tasklet
> busy-wait, something like:
>
> while not job_done():
>     stackless.schedule()

Yes.

> However I thought of another way, have the tasklet remove itself (via
> tasklet.capture) and have a seperate os-thread perform the job
> synchronously and insert the tasklet when the job is done. However I'm 
> not

Yes, this is indeed, a better way (especially if you need to using 
blocking I/O to
avoid busy-waiting).

> sure this is possible, hence the following questions:
>
> Does inserting a tasklet removed from thread A while in thread B 
> insert it
> in the scheduler list in A or in B?
> Are there even seperate scheduler lists for each thread?

Christian, if he has time, can give you a more complete answer, but it 
is
best to avoid doing operations between multiple threads that involve 
stackless.

Here is what you can do:

Create a (thread-safe) Queue object that contains the tasklets to 
insert back
into the runnable queue, and use a simple tasklet (running in the main 
thread)
to monitor and empty the queue - and re-insert the "blocked" tasklets.

-Jas

e.g.

# note: I didn't test this. Just typed it in off the top of my head...
import Queue
from stackless import *
done_tasks = Queue.Queue()

def func_that_blocks():
   def finish_in_other_thread(t):
     ...do stuff here...
     ...block hard if you want to...
     done_tasks.put(t)
   # do your capture dance to remove the current tasklet here...
   # something like this:
   t = tasklet().capture(getcurrent())
   t.remove()
   # start the thread to re-insert later...
   thread.start_new_thread(finish_in_other_thread, (t,))

def reinsert_tasklet():
   global done_tasks
   while 1:
     while done_tasks.qsize() > 0:
       t = done_tasks.get(0) # do not block!!!
       t.insert()
     schedule()







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



More information about the Stackless mailing list