I'm looking for a way to pass function arguments through another function, in a manner identical to Stackless' tasklet
instantiation:
stackless.tasklet(function_being_called)(*args)
So far the best way I've come up with is:
mylib.tasklet(function_being_called,*args)
which works, but is not identical to Stackless' syntax. I'm not sure where to look in the documentation to find out how to accomplish this (hence the rather ambiguous title for this question). Is this even possible, or is it part of Stackless' changes to the interpreter?
EDIT: I'm now aware that there is an approach which will work for functions, but I'm unsure if it will work in my case. I'm using the greenlet library: greenlet threads obtain args when the greenlet is switch()
ed to, not on instantiation. Calling them as shown below results in
TypeError: 'greenlet.greenlet' object is not callable
.
Using greenlet.greenlet(function(args))
(still not right syntax) executes immediately and still requires args in the switch()
method. Hence I currently store variables in-class, using the syntax shown above, to pass when calling switch()
. Hope this doesn't change the question too much!
As requested, here is the code in question. First, a variant on eri's answer (DISCLAIMER: I've never used decorators before):
import greenlet # Background "greenlet" threadlet library
_scheduled = [] # Scheduler queue
def newtasklet(func): # Returns a greenlet-making function & switch() arguments.
def inner(*args,**kwargs):
newgreenlet = greenlet.greenlet(func,None)
return newgreenlet,args,kwargs
return inner
class tasklet():
def __init__(self,function=None):
global _scheduled
initializer = newtasklet(function)
self.greenlet,self.variables,self.kvars = initializer()
_scheduled.append(self)
self.blocked = False
tasklet(print)("A simple test using the print function.")
Traceback (most recent call last):
File "<pyshell#604>", line 1, in <module>
tasklet(print)("A simple test using the print function.")
TypeError: 'tasklet' object is not callable
Original code (working but not syntactically ideal):
class tasklet():
def __init__(self,function=None,*variables,parent=None):
global _scheduled
self.greenlet = greenlet.greenlet(function,parent)
self.variables = variables
_scheduled.append(self)
self.blocked = False
>>> tasklet(print,"A simple test using the print function.")
<__main__.tasklet object at 0x7f352280e610>
>>> a = _scheduled.pop()
>>> a.greenlet.switch(*a.variables)
A simple test using the print function.
I've never used stackless before, but
stackless.tasklet(function_being_called)
is returning a function object that is being given the parameters(*args)
. If you want the syntax to be the same for your tasklet, thenmylib.tasklet
should return a function.For example:
Outputs: