Python multithreaded Programming Method analysis sample detailed _python

Source: Internet
Author: User

How to create a thread object in a python multi-thread

If you want to create a thread object, it's very simple as long as your class inherits threading. Thread, and then call threading first in the __init__. The __init__ method of thread can be

Copy Code code 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 do not want him to pull the empty, he has to give me a bit of real life. Simply, rewrite the run () method of the class to put all the things you do when you want to run the thread into it.

Copy Code code 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 lets this thread output information to the screen every 1 seconds after execution, 10 times after the end
GetName () is threading. A method of the thread class that is used to obtain the name of this thread object. There is another way SetName () of course is to set the name of this thread object.
If you want to create a thread, you first need to create a thread object
Mythreadmythread1 = Mythread (' Mythread 1′)
After a thread object is created, he is in the "Born" (birth state)
How do I get this Python multithreaded object to run? Just call the Thread object's start () method
Mythread1.start ()
The thread is now in the "Ready" state or also called the "runnable" state.
Is it strange? Didn't it start already? Why not be called "Running" state? Well, there's a reason for that. Because our computer generally does not have the real parallel processing ability. Our so-called Python multithreading just divides the time into fragments, then lets a thread execute it every other time, then goes into the "sleeping" state, and then wakes up another thread in "sleeping", so the loop runnable->sleeping- >runnable ..., just because the computer executes very quickly, and the time fragment interval is very small, we do not feel, thought to be at the same time. So a thread is only in the state of being able to run after start, and when he runs or is scheduled by the system.
When will that thread "dead"? Typically, the thread ends when the run method of the thread object finishes or throws an exception in execution. The "dead" state thread is automatically cleaned.
The T2 join () method can be invoked at T1 if a thread T1 to wait for another thread to T2 execution at the end of the process.

Copy Code code as follows:

def t1 (...):
...
T2.join ()

This allows T1 to wait for the T2 to finish after executing to the T2.join () statement.
But if T1 is a dead circle, then there is no point in waiting. A floating-point number can be timed out when the T2 join () method is invoked so that the thread does not wait for the flowers to be thanked. I wait for you 10s, you do not come back I do not allow me to remarry ah?

Copy Code code as follows:

def t1 (...):
...
T2.join (10)

If the main thread of a process runs out and the child threads are still executing, then the process will not quit until all the child threads are finished, so how do other child threads end up with the boss when the main thread ends? That's going to put those who are not obedient to the obedient younger brother, using the thread object's Setdaemon () method, the parameter is bool type. True on behalf of you to be obedient, my eldest brother (main thread), you have to follow the withdrawal, can not drag. If it is false, then do not have to be so obedient, the boss allows you to be in the army will not be affected by life. Note that the Setdaemon () method must be invoked before the thread object calls the start () method, otherwise it will not work.

Copy Code code 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 you execute 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, even if the main thread ends, wait for the T1 thread to end itself before exiting the process. Isdaemon () is used to obtain the 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 threads that start the Run method but are not terminated)
Threadlist = Threading.enumerate ()

Get the length of this list
ThreadCount = Threading.activecount ()
View the state of a thread object calls the IsAlive () method of this thread object, and returns 1 representing the "runnable" state with no "dead"

Threadflag = Threading.isalive ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.