views:

1988

answers:

3

Given the documentation at http://docs.python.org/library/threading.html which states for Thread.run():

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

I have constructed the following code:

class DestinationThread(threading.Thread):
    def run(self, name, config):
        print 'In thread'

thread = DestinationThread(args = (destination_name, destination_config))
thread.start()

But when I execute it, I receive the following error:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
    self.run()
TypeError: run() takes exactly 3 arguments (1 given)

It seems I am missing something obvious, but the various examples I have seen work with this methodology. Ultimately I am trying to just pass the string and dictionary into the thread, if the Constructor is not the right way, but rather to make a new function to set the values prior to starting the thread, I am open to that.

Any suggestions on how to best accomplish this?

A: 

You define the run method to accept 3 arguments, but you call it with one argument (python calls it with the reference to the object).

You need to pass the arguments to run instead of __init__.

Or make the __init__ method accept the arguments instead.

Vasil
Are you sure that's right? I didn't think you could pass any arguments to start().
John Fouhy
You can't, I meant run. Thanks.
Vasil
No, this doesn't work. You can't pass arguments to start(), and the whole point is you can't call run() directly.
Casey
+10  A: 

You really don't need to subclass Thread. The only reason the API supports this is to make it more comfortable for people coming from Java where that's the only way to do it sanely.

The pattern that we recommend you use is to pass a method to the Thread constructor, and just call .start().

 def myfunc(arg1, arg2):
     print 'In thread'
     print 'args are', arg1, arg2

 thread = Thread(target=myfunc, args=(destination_name, destination_config))
 thread.start()
Jerub
This was a key piece of info I was missing, which is why I accepted it, but in my case, I actually am extending the class adding needed functions to pull data out of the thread.
Crad
A thousand times yes. I have to explain this one a lot to Java developers.
Ali A
+3  A: 

Turns out it was init I needed to override:

class DestinationThread(threading.Thread):
    def __init__(self, name, config):
        threading.Thread.__init__(self)        
        # Set internal variables
        self.name = name
        self.config = config
    def run(self):
      print 'In thread'
Crad