How to Create a thread object in Python Multithreading
If you want to create a Thread object, it is very simple, as long as your class inherits threading. Thread, and then in _ init _, You can first call the _ init _ method of threading. Thread.
Copy codeThe Code is as follows:
Import threading
Class mythread (threading. Thread ):
Def _ init _ (self, threadname ):
Threading. Thread. _ init _ (self, name = threadname)
This is just an empty thread. I don't want him to pull the empty car. He has to do some real work for me. It's easy to rewrite the run () method of the class and put all the things you want to do during thread execution into it.
Copy codeThe Code is as follows:
Import threading
Import time
Class mythread (threading. Thread ):
Def _ init __(...) :
....
Def run (self ):
For I in range (10 ):
Print self. getName, I
Time. sleep (1)
The above Code allows this thread to output information to the screen every 1 second after execution and end after 10 times.
GetName () is a method of the threading. Thread class to obtain the name of this Thread object. Another method, setName (), is to set the name of the thread object.
To create a thread, you must first create a thread object.
Mythreadmythread1 = mythread ('mythread 1 ′)
After a thread object is created, it is in the "born" state)
How to run this Python multi-threaded object? You only need to call the start () method of the thread object.
Mythread1.start ()
Now the thread is in the "ready" or "runnable" state.
Strange? Isn't it already started? Why is it not called the "running" status? There is actually a reason. Because our computers generally do not have real parallel processing capabilities. The so-called Python multithreading only divides the time into segments, then let a thread execute each time period, and then enter the "sleeping" state, and then wake up another thread in the "sleeping" state, so loop runnable-> sleeping-> runnable... It's just because the computer runs fast, and the interval between time segments is very small. We don't feel it. We thought it was done at the same time. Therefore, after a thread starts, it is only in a running state. When will it be scheduled by the system.
When will a thread "dead? Generally, when the run method of the thread object ends or an exception is thrown during execution, the thread ends. The system automatically clears the "dead" status thread.
If one thread t1 needs to wait until the execution of the other thread t2 ends before it can run, then t1 can call the join () method of t2.
Copy codeThe Code is as follows:
Def t1 (...) :
...
T2.join ()
In this way, t1 will continue to run after the t2.join () Statement is executed.
But if t1 is an endless loop, then it makes no sense to wait. What should we do? You can set a timeout parameter for a floating point number when calling the t2 join () method, so that this thread will not wait for the flowers to thank you. I am waiting for you for 10 s. If you don't come back, I'm not allowed to be remarried yet?
Copy codeThe Code is as follows:
Def t1 (...) :
...
T2.join (10)
If the main thread of a process is running and the sub-thread is still executing, the process will not exit until all sub-threads are finished, how can we let other sub-threads retreat with the boss when the main thread ends? Then we need to set the disobedient persons as obedient younger siblings and use the setDaemon () method of the thread object. The parameter is of the bool type. If it is True, you will be obedient. If it is True, the boss (the main thread) will call and you will also withdraw from the system. If it is False, you don't have to be so obedient. The boss allows you to fight for something that will not happen. Note that the setDaemon () method must be called before the thread object does not call the start () method. Otherwise, it will not work.
Copy codeThe Code is as follows:
T1 = mythread ('t1 ′)
Print t1.getName (), t1.isDaemon ()
T1.setDaemon (True)
Print t1.getName (), t1.isDaemon ()
T1.start ()
Print 'main thread exit'
When print 'main thread exit 'is executed, the main thread exits, and the thread t1 ends. However, if you do not use the setDaemon () method of the t1 thread object, even if the main thread ends, you have to wait for the t1 thread to stop before exiting the process. IsDaemon () is used to obtain the Daemonflag status of a thread object.
How to obtain information related to Python multithreading?
Get the reference of the currently running thread
Running = threading. currentThread ()
Obtains a list of all the currently active objects (that is, any threads that start but have not terminated the run method ).
Threadlist = threading. enumerate ()
Get the length of this list
Threadcount = threading. activeCount ()
Check the status of a thread object and call the isAlive () method of this thread object. If 1 is returned, it indicates that it is in the "runnable" State and there is no "dead"
Threadflag = threading. isAlive ()