Question

import stackless
class MyTasklet(stackless.tasklet):
    def __init__(self, func, msg):
        pass

def foo():
    pass

msg = 'hello'
MyTasklet(foo, msg)()

I am using stackless python, this code generates the following error:

Traceback (most recent call last):
  File "E:/workspace/python/test/klass.py", line 11, in <module>
    MyTasklet(foo, msg)()
TypeError: tasklet() takes at most 1 argument (2 given)

It's very odd since I did not call the constructor of stackless.tasklet.

Anyone know what exactly the error is about.

Was it helpful?

Solution

Since the problem has to do with sub-classing stackless.tasklet, you could just make MyTasklet a callable object instead like follows:

import stackless
class MyTasklet(object):
    def __init__(self, func, msg):
        self.func = func
        self.msg = msg 

    def __call__(self):
      t = stackless.tasklet()
      t.bind(self.func)
      t.setup(self.msg)
      t.run()

def foo(msg):
  print msg 

msg = 'hello'
MyTasklet(foo, msg)()

EDIT: Alternatively you can just override __new__ to pass the correct arguments to stackless.tasklet.__new__:

import stackless
class MyTasklet(stackless.tasklet):
  def __new__(cls, func, msg):
    return stackless.tasklet.__new__(MyTasklet, func)
  def __init__(self, func, msg):
    pass

def foo():
  pass

msg = 'hello'
MyTasklet(foo, msg)()

OTHER TIPS

Do not subclass stackless.tasklet.

It is not how you are meant to use Stackless Python - and more importantly, it is difficult and overly complicated compared to the easier way. Anything you need to do, you should be able to do in f, the function you run in the tasklet. If you do really really want to subclass anyway, search for existing code examples, which will already give you a basic working tasklet subclass.

If you really want help with this, no-one with Stackless Python experience really answers questions here. The Stackless Python mailing list is where they are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top