0
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.

1
  • 2
    The problem is that stackless.tasklet implements __new__ and its __new__ method accepts only an argument. Commented Dec 30, 2013 at 10:01

2 Answers 2

0

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)()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, but I am not asking for workarounds, I want to know why my code failed.
the reason you're code is failing is because stackless.tasklet.__new__ takes only one parameter but you haven't overridden __new__ in your subclass. I've edited my answer to reflect your request (i.e. a solution which keeps the subclassing)
got it, the constructor is in fact __new__ not __init__.
0

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.

1 Comment

subclassing stackless.tasklet is fine, one just have to know which methods to override if one is going to change the arguments to the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.