How to create a thread object in Python multi-threading
If you want to create a thread object, it is simple, as long as your class inherits threading. Thread, and then call threading first in the __init__. The __init__ method of thread can
The 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 wagon, he has to do some real work for me. It's easy to rewrite the run () method of the class to put everything you need to do when the thread executes.
The 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 we let this thread after execution every 1 seconds output information to the screen, 10 times after the end
GetName () is threading. A method of the thread class that is used to obtain the name of this threading object. There is also a way setname () of course is to set the name of this thread object.
If you want to create a thread, first create a thread object
Mythreadmythread1 = Mythread (' Mythread 1′)
Once a thread object is created, he is "born" (The birth state)
How do I get this Python multithreaded object to run? As long as the start () method of the thread object is called
Mythread1.start ()
The thread is now in the "Ready" state or also known as the "runnable" state.
Is it strange? Isn't it already start? Why not call it the "running" state? There are, in fact, a reason. Because our computers are generally not capable of real parallel processing. Our so-called Python multithreading just divides the time into fragments, then lets one thread execute it every other time, then goes into the "sleeping" state, then wakes up another thread in "sleeping", so looping runnable->sleeping- >runnable ..., just because the computer executes very fast, and the time fragment interval is very small, we do not feel, think is at the same time. So a thread is only in a running state after start, and when he runs or is scheduled by the system.
When will that thread "dead"? In general, when the run method of a thread object finishes executing or throws an exception in execution, the thread ends. The "Dead" status thread is automatically cleaned up by the system.
If a thread T1 in the process of execution and waits for another thread to T2 execution to finish before it can run, then it is possible to T1 the join () method called T2
The code is as follows:
def t1 (...):
...
T2.join ()
This T1 waits for the T2 to finish after the T2.join () statement is executed before it continues to run.
But if T1 is a dead loop then there is no point in waiting, what then? You can give a floating-point number a timeout parameter when calling T2 's join () method, so the thread won't wait for the flowers to thank you. I wait for you 10s, you do not come back I still do not allow me to remarry ah?
The code is as follows:
def t1 (...):
...
T2.join (10)
If the main thread of a process has finished running and the child threads are still executing, then the process will not exit until all the child threads have ended and the other child threads will retreat with the eldest when the main thread ends. It is necessary to set those who do not obey the obedient brother, using the thread object's Setdaemon () method, the parameter is bool type. True if you want to obey, my boss (main thread), you have to follow the withdrawal, can not drag. If it is false then do not be so obedient, the boss allows you to be in the foreign military life is not affected. It is important to note that the Setdaemon () method must be called before the start () method is called by the thread object, otherwise no effect.
The 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 executed to print ' main thread exit ', the main thread exits, and of course T1 is finished. However, if you do not use the Setdaemon () method of the T1 thread object, wait for the T1 thread to end itself to exit the process, even if the thread ends. Isdaemon () is used to obtain a daemonflag state of a thread object.
How do I get information about Python multithreading?
Get a reference to the currently running thread
running = Threading.currentthread ()
Gets a list of all currently active objects (that is, any thread that the Run method starts but does not terminate)
Threadlist = Threading.enumerate ()
Get the length of this list
ThreadCount = Threading.activecount ()
View the state of a thread object call the IsAlive () method of this thread object, return 1 for the "runnable" state and No "dead"
Threadflag = Threading.isalive ()