scrapy scrapy - 2 months ago 14
Python Question

What does the _MainThread mean here for thread.run()?

Here is a python code snippet.

import threading

class myThread(threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter

def run(self):
currentTreadname = threading.currentThread()
print "running in", currentTreadname


Let's instance class myThread

thread = myThread(1,"mythrd",1)


And call run method in it.

thread.run()
running in <_MainThread(MainThread, started 139982993762048)>


What does the _MainThread mean here for thread.run() ?
In the manual of threading:

The standard run() method invokes the callable object passed to the object’s constructor as the target argument.


1.Here are two objects, for my example, the first one callable object means
_MainThread
?

The second object in
to the object’s constructor
means myThread’s constructor ,right or not?

2.Why run method in son class can invoke parent class?

thread.run()
running in <_MainThread(MainThread, started 139982993762048)>


thread is the instance of son class--myThread(1,"mythrd",1),why thread.run() revoke a _MainThread ?

Answer Source

You should call the Thread.start() method.

import threading  

class myThread(threading.Thread):
    def __init__(self,threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):                                                                                                                                                                            
        currentTreadname = threading.currentThread()
        print "running in", currentTreadname

thread = myThread(1, "mythread", 1)
thread.start()

The output is :

running in <myThread(mythread, started 140140930873088)>

start()

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

run()

Method representing the thread’s activity.

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.

So as the doc said, start() method start the thread's activity, yes, this activity is run() method.

update

The standard run() method invokes the callable object passed to the object’s constructor as the target argument.

The second object is your myThread instance, you are right. But the callable object is not you said. check this threading.Thread definition:

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

The callable object is the target parameter you pass it. more about this you can refer https://docs.python.org/2/library/threading.html

And why your code print running in <_MainThread(MainThread, started 139982993762048)>?

Because you do not call the Thread.start method, so it will not start a new thread. So if you just call the myThread.run, It is a just normal object method calling. It is called in mainThread. So it will print this.