Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding python threading.Thread.run()

Given the Python documentation 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?

like image 833
Gavin M. Roy Avatar asked Mar 19 '09 03:03

Gavin M. Roy


People also ask

Why is it important to override the run () method in a thread class?

It is highly recommended to override run() method because it improves the performance of the system. If we don't override Thread class run() method in our defined thread then Thread class run() method will be executed and we will not get any output because Thread class run() is with an empty implementation.

How do you extend a thread in Python?

Thread class can be extended to run code in another thread. This can be achieved by first extending the class, just like any other Python class. Then the run() function of the threading. Thread class must be overridden to contain the code that you wish to execute in another thread.

Why do we override the run method?

The reason why we override run when we extend the thread class is that we want some piece of code to run in a multi-threaded fashion. So the creators of Java have agreed upon a name for the method to be overridden.

Can multiple threads run at the same time Python?

Like all modern programming languages, Python also allows you to implement multithreading in your applications. In this article, you'll see how you can implement multithreading with Python and we'll show examples of how multithreading can be used to run multiple functions in parallel.


1 Answers

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() 
like image 130
Jerub Avatar answered Oct 16 '22 20:10

Jerub