Python multithreaded programming

Source: Internet
Author: User

Talking about multithreading , must think of the process, lock and other things, Python and thread-related modules have thread module,threading module,Queue module,mutex modules,socketserver modules. The thread module provides basic thread and lock support, but is not recommended because when the main thread exits, all other threads are not purged and exits, while the threading module provides a more powerful thread management capability to ensure that all important child threads are exited before the process ends. The queue module allows a user to create a data structure that can be used to share information between multiple threads. A mutex is a mutex object that socketserver a TCP and UDP manager with thread control.

If the thread module is used, before start_new_thread creates a new thread, it may be allocate_lock to assign a LockType type lock object, and then go acquire get the lock object, after creating the thread, After doing something in a new thread, you can release the lock, usually checking the locked state of each lock in the main thread, when all locks are released and the main thread ends.

The threading module does not have to operate the lock like the thread module, only the join causes the program to hang. The thread class in the threading module is the main running object and is commonly used in several similar usages, such as passing a thread instance to a function or a callable class object (which can be implemented by apply () _ _call__()). or derive a subclass from thread (override run ()) and create an instance of the subclass. Here is a simple example of creating two threads, passing in functions in thread instances, SLEEP6, 3 wonderful, and the main thread showing the total elapsed time.

#!/usr/bin/env pythonImportThreading fromTimeImportSleep, ctimeloops = [6,3]# Sleep sec def Loop(Nloop, nsec):    Print ' Start loop ', Nloop,' at: ', CTime () sleep (nsec)Print ' Loop ', Nloop,' done at: ', CTime () def main():    Print ' starting at: ', CTime () threads = [] Nloops = range (len (loops)) forIinchNloops:t = Threading. Thread (target = loop, args = (i, loops[i])) Threads.append (t) forIinchNloops:threads[i].start () forIinchNloops:threads[i].join ()Print ' All do at : ', CTime ()if__name__ = =' __main__ ': Main ()

Here is the result of the operation:
Starting At:tue June 9 09:56:32 2015
Start Loop 0 At:tue June 9 09:56:32 2015
Start Loop 1 At:tue June 9 09:56:32 2015
Loop 1 done at:tue June 9 09:56:35 2015
Loop 0 done At:tue June 9 09:56:38 2015
All done At:tue June 9 09:56:38 2015
As you can see, the main thread time is the time of the child thread for a long time, 6 seconds.

Python multithreaded programming

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.