[Stackless] Giving names for tasklets
Stephan Diehl
stephan.diehl at gmx.net
Tue Jun 27 17:45:42 CEST 2006
Juho Mäkinen schrieb:
> Does anybody have any good idea how to give names for each individual tasklets?
>
> One way would be to create a wrapper function like
> def createTasklet(func, name, *args, **kw)
> where func is the function which is to be started as tasklet, name
> is the name of the tasklet and the rest are arguments passed
> to the func. The only question is how to implement this. Any ideas?
>
> - Juho Mäkinen
As Christopher already mentioned, you'll probably want to use
subclassing (since you can't just stick random attributes to 'tasklets').
The following would do (for example):
----------------------------------------------------
import stackless
global_counter = 0
class NamedTasklet(stackless.tasklet):
def __new__(cls, func, name=''):
return super(NamedTasklet,cls).__new__(cls, func)
def __init__(self, func, name=''):
global global_counter
global_counter += 1
if not name:
name = 'T%s' % global_counter
self.name = name
super(NamedTasklet,self).__init__(func)
def __str__(self):
return 'NamedTasklet("%s")' % self.name
if __name__ == '__main__':
def f():
print 'f'
stackless.schedule()
def g():
print 'g'
tf = NamedTasklet(f,'tf')()
tg = NamedTasklet(g)()
stackless.run()
print tf
print tg
-----------------------------------------------------------------
_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless
More information about the Stackless
mailing list