Python High performance programming--003--thread threads and threading

Source: Internet
Author: User
Tags thread stop win32

First, the thread base
Python supports multithreaded programming when running on most Unix-like systems, such as Win32 and Linux, Solaris, MacOS, BSD, and so on. Python uses POSIX-compliant threads, known as pthreads.

By default, the source code is installed in the version 2.0 and above of Python;
Win32 in the installation package;
The thread is open by default.

bogon:~ elaine$ pythonPython 2.7.10 (default, Feb  

Import the thread directly under the command line, and if no error is indicated, the thread support is turned on.

If your Python interpreter does not open thread support at compile time, the import module will fail, in which case you will have to recompile your Python interpreter to use the thread.
You can add the "-with-thread" parameter when you run the configuration script.

Conditions for wireless support:
onethr.py

#!/usr/bin/pythonfrom time import sleep,ctimedef loop0():    print ‘start loop 0 at:‘,ctime()    sleep(4)    print ‘loop 0 done at:‘,ctime()def loop1():    print ‘start loop 1 at:‘,ctime()    sleep(2)    print ‘loop 1 done at:‘,ctime()def main():    print ‘starting at:‘,ctime()    loop0()    loop1()    print ‘all DONE at:‘,ctime()if __name__ == ‘__main__‘:    main()

Output Result:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /data/study/python/project/test/onethr.pystarting at: Mon Mar 26 09:21:46 2018start loop 0 at: Mon Mar 26 09:21:46 2018loop 0 done at: Mon Mar 26 09:21:50 2018start loop 1 at: Mon Mar 26 09:21:50 2018loop 1 done at: Mon Mar 26 09:21:52 2018all DONE at: Mon Mar 26 09:21:52 2018Process finished with exit code 0

From the above output can be seen, in the absence of thread support, that is, in a single-threaded execution, the above code has two loops, a loop loop0 end, another loop loop1 execution, the entire program run time is two cycles of the sum of the run time (6s).
In fact, the two loops in the above code can be run concurrently, so that the entire program runs for the longest cycle of the run time.
If the loop0 run time is 4s,loop1 run time is 2s, the entire program run time is 4s.
In order to improve the operation efficiency of the program, Python has multithreaded programming.

Python has several modules for multithreaded programming: thread,threading, queue, and so on.
The thread module provides basic threading and lock support;
The Threading module provides basic threading and lock support, as well as higher-level, more powerful thread management capabilities;
The queue module is a queuing data structure that enables data sharing between multiple processes.

Second, the thread module
In a real project, the thread module is not recommended because the module has a fatal flaw:

When the main thread ends, all threads are forced to end, without any alarms and normal thread cleanup work, which is unacceptable.

Module functions:

Start_new_thread ()
Creates a new thread that invokes the function with the specified parameters and optional Kwargs in the new thread.

Allocate_lock ()
Assigns a lock object of type LockType.

Exit ()
Let the thread exit.

LockType type Lock Object method:

Acquire ()
Try to get the lock object.

Locked ()
Returns true if the lock object is acquired, otherwise false.

Release ()
Release the lock.

#!/usr/bin/pythonfrom time import sleep,ctimeimport threaddef loop0():    print ‘start loop 0 at:‘,ctime()    sleep(4)    print ‘loop 0 done at:‘,ctime()def loop1():    print ‘start loop 1 at:‘,ctime()    sleep(2)    print ‘loop 1 done at:‘,ctime()def main():    print ‘starting at:‘,ctime()    thread.start_new_thread(loop0,())    thread.start_new_thread(loop1,())    sleep(6)    print ‘all DONE at:‘,ctime()if __name__ == ‘__main__‘:    main()

Output Result:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /data/study/python/project/test/onethr.pystarting at: Mon Mar 26 13:10:30 2018start loop 0 at: Mon Mar 26 13:10:30 2018start loop 1 at: Mon Mar 26 13:10:30 2018loop 1 done at: Mon Mar 26 13:10:32 2018loop 0 done at: Mon Mar 26 13:10:34 2018all DONE at: Mon Mar 26 13:10:36 2018Process finished with exit code 0

Start_new_thread (Loop0, ()) is a minimum of two parameters, one is a function, and the other is the value passed to the function.
As can be seen from the above output, loop0 and LOOP1 are run concurrently, that is, concurrent execution.
There is one line of code in the main thread: Sleep (6)
This line of code to stop the main thread, because if there is no line of code, the main process after running two sub-threads, the main thread will not wait for the sub-thread execution to continue to execute, which will cause the child threads do not complete, the main thread directly print output "all do", It is undesirable to start the two sub-processes that will passively exit the end.
In this code, we did not let the main thread stop waiting for all the sub-threading technology before continuing to run the rest of the code.
Sleep (6) function is a synchronization mechanism.

There should be a thread management approach, rather than using sleep as an unreliable synchronization mechanism.
Does not allow the main thread to exit prematurely or late, introducing a lock mechanism.

loops=[4,2]def loop0(nloop,nsec,lock):    print ‘start loop ‘,nloop,‘at:‘,ctime()    sleep(nsec)    print ‘loop ‘,nloop,‘done at:‘,ctime()    lock.release()def main():    print ‘starting at:‘,ctime()    locks = []    nloops = range(len(loops))    for i in nloops:        lock = thread.allocate_lock()        lock.acquire()        locks.append(lock)    for i in nloops:        thread.start_new_thread(loop0,(i,loops[i],locks[i]))        sleep(1)    for i in nloops:        while locks[i].locked():pass    print ‘all DONE at:‘,ctime()if __name__ == ‘__main__‘:    main()

Output Result:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /data/study/python/project/test/onethr.pystarting at: Mon Mar 26 13:32:09 2018start loop  0 at: Mon Mar 26 13:32:09 2018start loop  1 at: Mon Mar 26 13:32:10 2018loop  1 done at: Mon Mar 26 13:32:12 2018loop  0 done at: Mon Mar 26 13:32:13 2018all DONE at: Mon Mar 26 13:32:13 2018Process finished with exit code 0

Three, threading module

Python High performance programming--003--thread threads and threading

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.