Using multi-threaded methods
1, Function: Use threading module Threading. Thread (e.g target name parameters)
1 Importtime,threading2 defLoop ():3 Print("thread%s is running ..."%Threading.current_thread (). Name)4n =05 whileN < 5:6n + = 17 Print("thread%s is running ... n =%s"%(Threading.current_thread (). NAME,STR (n)))8Time.sleep (1)9 Print("Thread%s is-over ..."%Threading.current_thread (). Name)Ten One Print("thread%s is running ..."%Threading.current_thread (). Name) A -TS = [] - forIinchRange (5): thet = Threading. Thread (target = loop, name ='Loopthread'+str (i)) - T.start () - ts.append (t) - forTinchTS: + T.join () - Print("Thread%s is-over ..."% Threading.current_thread (). Name)
Multi-threaded output:
Thread Mainthread isrunning...thread Loopthread 0 isrunning...thread Loopthread 0 isRunning ... n = 1Thread Loopthread1 isRunning...thread Loopthread1 isRunning ... n = 1Thread Loopthread2 isRunning...thread Loopthread2 isRunning ... n = 1Thread Loopthread 0 isRunning ... n = 2Thread Loopthread1 isRunning ... n = 2Thread Loopthread2 isRunning ... n = 2Thread Loopthread 0 isRunning ... n = 3Thread Loopthread1 isRunning ... n = 3Thread Loopthread2 isRunning ... n = 3Thread Loopthread 0 isRunning ... n = 4Thread Loopthread1 isRunning ... n = 4Thread Loopthread2 isRunning ... n = 4Thread Loopthread 0 isRunning ... n = 5Thread Loopthread1 isRunning ... n = 5Thread Loopthread2 isRunning ... n = 5Thread Loopthread 0 isOver...thread Loopthread1 isOver...thread Loopthread2 isOver...thread Mainthread isOver ...
Some of the mechanisms of thread in Python are different from C + +: in C + +, when the main thread ends, its child threads are killed by default by the main thread. In Python, after the main thread ends, it waits for the child thread to end by default, and the mainline friend exits.
Python has two functions for thread management: Join and Setdaemon
Join: If Threada.join () is called in a thread B, thread B will then run back Threada.join () after Threada ends.
Setdaemon: Main thread A starts child thread B, calls B.setdaemaon (True), and then thread B kills when the main thread ends. "This excerpt from Junshao90 's blog"
2. Use object-oriented mode. Create subclasses inherit from threading. Thread, you need to overwrite the Run method
1 Importtime,threading2 classThreadTest (Threading. Thread):3 def __init__(self,tname):4Threading. Thread.__init__(self)5Self.name =Tname6 defRun (self):7 Print("thread%s is running ..."%Threading.current_thread (). Name)8n =09 whileN < 5:Tenn + = 1 One Print("thread%s is running ... n =%s"%(Threading.current_thread (). NAME,STR (n))) ATime.sleep (1) - Print("Thread%s is-over ..."%Threading.current_thread (). Name) - Print("thread%s is running ..."%Threading.current_thread (). Name) the - forIinchRange (3): -t = threadtest ('T'+str (i)) - T.start () + T.join () - Print("Thread%s is-over ..."% Threading.current_thread (). Name)
Run output:
Thread Mainthread isRunning...thread t0 isRunning...thread t0 isRunning ... n = 1Thread T0 isRunning ... n = 2Thread T0 isRunning ... n = 3Thread T0 isRunning ... n = 4Thread T0 isRunning ... n = 5Thread T0 isOver...thread T1 isRunning...thread T1 isRunning ... n = 1Thread T1 isRunning ... n = 2Thread T1 isRunning ... n = 3Thread T1 isRunning ... n = 4Thread T1 isRunning ... n = 5Thread T1 isOver...thread T2 isRunning...thread T2 isRunning ... n = 1Thread T2 isRunning ... n = 2Thread T2 isRunning ... n = 3Thread T2 isRunning ... n = 4Thread T2 isRunning ... n = 5Thread T2 isOver...thread Mainthread isOver ...
3. Lock
The biggest difference between multithreading and multi-process is that, in many processes, the same variable, each with a copy in each process, does not affect each other.
In many threads, all variables are shared by all threads, so any one variable can be modified by any one thread, so the biggest danger of sharing data between threads is that multiple threads change a variable at the same time, altering the content.
Lock object:
Acquire (): responsible for acquiring a lock. If there is no line is impersonating holding the lock, the acquire method will immediately get the lock. Otherwise, its idle state locks are released. Once acquire () returns, the thread that invokes it holds the lock.
Release (): Releases the lock. If other threads are waiting for this lock (via acquire ()), when release () is in effect, one of the threads will
Be awakened
The following is excerpted from the official website of Liu Xuefeng.
Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001386832360548a6491f20c62d427287739fcfa5d5be1f000
Balance for shared resources, multi-process execution at the same time, a certain probability result for balance! = 0[Detailed description in the original)
def change_it (n): # The result should be 0 if you save it first: Global Balance = balance + n = balance-n
Use Threading. Lock ()
ImportThreadingtotal=0lock=Threading. Lock ()defChange (n):Global Total Total+=N Total-=Ndefrun_thread (N): Lock.acquire () forIinchRange (100000): Change (n) lock.release () T1= Threading. Thread (target = Run_thread, args= (5,)) T2= Threading. Thread (target = Run_thread, args= (8,)) T1.start () T2.start () T1.join () T2.join ( )Print(total)
4. Other detailed information on the process can be consulted
Resolving shared resource issues: condition variables, synchronizing queues
Vamei's blog Python standard library 08 multi-Threading and Synchronization (threading package)
Slice Inspired blog Python multi-threaded learning
"Python,threading" Python multithreading