I. About Python multithreading
The Python interpreter can run multiple threads at the same time, but only one thread will run in the interpreter at any given time.
Access to the Python virtual machine is controlled by the global Unlock (Gil), which guarantees that only one thread is running at the same time.
This is done in the following ways:
1. Set Gil
2. Switch into a Process execution
3. Perform one of the following actions
A. Running a specified number of byte codes ( clock -controlled in the operating system)
B. Thread actively assign control
4. Set the thread to sleep, that is, switch the
5. Unlock Gil
6. Repeat the above steps
Note: 1. When external code is called (built-in functions for C + + extensions), the Gil remains locked because the external code does not have a python bytecode.
2.I/O-intensive Python programs make better use of multithreading than computationally intensive programs.
Two. Multithreading on the THEAD module:
Example 1, when multi-threading is not used:
1 fromTimeImportSleep, CTime2 3 defLoop ():4 Print('start loop0 at:', CTime ())5Sleep (4)6 Print('Loop 0 done at:', CTime ())7 8 defLoop1 ():9 Print('start LOOP1 at:', CTime ())TenSleep (2) One Print('Loop1 done at:', CTime ()) A - defMain (): - Print('starting at:', CTime ()) the Loop () - Loop1 () - Print('Loop1 done at:', CTime ()) - + if __name__=='__main__': -Main ()
The output is:
Starting At:mon Dec 19 22:17:42 2016
Start loop0 At:mon Dec 19 22:17:42 2016
Loop 0 Done At:mon Dec 19 22:17:46 2016
Start Loop1 At:mon Dec 19 22:17:46 2016
Loop1 done At:mon Dec 19 22:17:48 2016
Loop1 done At:mon Dec 19 22:17:48 2016
From the results we can see that loop0 and loops are executed in sequence, taking a total of 6s
The core function of the thread module is: Start_new_thread ()
Usage: _thread.start_new_thread (func, *arg, **kwargs)
Example 2, when using Multithreading:
1 Import_thread2 fromTimeImportSleep, CTime3 4 defloop0 ():5 Print('start loop0 at:', CTime ())6Sleep (4)7 Print('loop0 done at:', CTime ())8 9 defLoop1 ():Ten Print('start LOOP1 at:', CTime ()) OneSleep (2) A Print('Loop1 done at:', CTime ()) - - defMain (): the Print('starting at:', CTime ()) - _thread.start_new_thread (Loop0, ()) - _thread.start_new_thread (LOOP1, ()) -Sleep (6) + Print('All do at :', CTime ()) - + A if __name__=='__main__': atMain ()
The result is:
Starting At:mon Dec 19 22:27:57 2016
Start loop0 At:mon Dec 19 22:27:57 2016
Start Loop1 At:mon Dec 19 22:27:57 2016
Loop1 done At:mon Dec 19 22:27:59 2016
Loop0 done At:mon Dec 19 22:28:01 2016
All done At:mon Dec 19 22:28:03 2016
As can be seen from the results, 1. The result is not output in sequence, Loop0 end
2.loop0 and Loop1 run time is 4s, than not using multithreading faster 2s
3.sleep (6) is for the main thread, LOOP0 and LOOP1 are expected to be executed before 6s.
If we set the execution time of the main thread to 3s, then the result is interesting.
Starting At:mon Dec 19 22:33:01 2016
Start loop0 At:mon Dec 19 22:33:01 2016
Start Loop1 At:mon Dec 19 22:33:01 2016
Loop1 done At:mon Dec 19 22:33:03 2016
All done At:mon Dec 19 22:33:04 2016
From this result oh can be seen, loop0 did not complete the forced exit!!! The reason is that the cut-out time for the main thread is 3s,3s
After the main thread continues to execute, loop0 even if not finished, forced to quit!!
As can be seen from here, the main thread to perform sleep () to achieve the purpose of synchronization is not reliable, so the concept of the introduction of locks.
Example 3, multithreading with locks:
1 Import_thread2 fromTimeImportSleep, CTime3 4loops = [4, 2]5 6 7 defLoop (Nloop, Nsec, lock):8 Print('Start Loop', Nloop,'At :', CTime ())9 Sleep (nsec)Ten Print('Loop', Nloop,' done at', CTime ()) One lock.release () #释放锁 A - - defMain (): the Print('starting at:', CTime ()) -Locks = [] - - forIinchRange (len (loops)): +Lock =_thread.allocate_lock () - lock.acquire () #锁上锁 + Locks.append (lock) #保存锁 A at forIinchRange (len (loops)): - _thread.start_new_thread (Loop, (I, loops[i], locks[i])) - - forIinchRange (len (loops)): - whileLocks[i].locked ():Pass #使用忙等待暂停主线程 until all the locks are released to continue the main thread, - in Print(' all do at', CTime ()) - to if __name__=='__main__': +Main ()
Results:
Starting At:mon Dec 19 22:52:02 2016
Start Loop 0 At:mon Dec 19 22:52:02 2016
Start Loop 1 At:mon Dec 19 22:52:02 2016
Loop 1 done at Mon Dec 19 22:52:04 2016
Loop 0 done at Mon Dec 19 22:52:06 2016
All do at Mon Dec 19 22:52:06 2016
As can be seen from the results, using the multi-threaded, run time is 4s, and do not have to manually set the main thread cut out time.
Reference: Python Core Programming-fourth Chapter Wesley Chun
"[email protected]" thread module