Hi.

My target is emulate real threading .

Code with cooperative threads looks like this:

from greenlet.tasklet import *

def demoTask(task, name):
    for x in xrange(20):
        print "Hello i'm %s" % name
        yield wait(10) # seconds / 10
    task.result = "Hello from %s" % name


def mainTask(task):
  t1 = Tasklet(demoTask, "Task 1")
  t1.run()
  #you can also use spawn 
  t2 = spawn(demoTask, "Task 2")
  yield join([t1,t2]) # wait forever t1 and t2 end
  #yield join([t1,t2], 300) # is the same with 30 seconds timeout (Timeout 
exception is Raised inside generator)
  #yield join([t1,t2]) + timeout(300) # the same above, you can concatenate 
events.
  print "t1 and t2 finished"
  yield wait(10) # wait function don't raise exception Timeout.
  print "t1 result: %s" % t1.result
  print "t1 result: %s" % t2.result


if __name__ == '__main__':

  mt = spawn(mainTask)





Exceptions if not catch, are propagated to caller generator.

The tasklet library is ready, i'll document it and then publish.


Bye.







El lunes, 17 de septiembre de 2012 16:21:28 UTC+2, Vsevolod Fedorov 
escribió:
>
>  On 09/13/12 20:58, Pepe Aracil wrote: 
>
> I'm working on a new threading library for pyjs based on generators. This 
> effort is an attempt to escape from asynchronous Hell.  
>
>  The main goal is simulate threading (cooperative threading)  with 
> funtions like  spawn, join, kill, wait, ....
>
>  Are there any other project like this to join efforts?
>  
>
> May be the twisted project (http://twistedmatrix.com/trac/) with its 
> @inlineCallbacks decorator is worth looking at. It uses generators to wrap 
> cooperative threading.
> But I must note that working with it was very unpleasant experience...
>
> Seva
>
>  

-- 



Reply via email to