"Python" Python multithreaded Programming

Source: Internet
Author: User

Speaking of multithreading , must have thought of the process, lock and other things, Python and threading module has thread module,threading module,Queue module,mutex module,socketserver module. The thread module provides basic thread and lock support, but not recommended, because when the main thread exits, all other threads exit without being purged, and the threading module provides more powerful threading management capabilities to ensure that all important child threads are exited before the process ends. The queue module allows users to create a queue data structure that can be used to share data between multiple threads. A mutex is a mutex object that socketserver TCP and UDP managers with thread control.

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

The threading module does not have to operate the lock as the thread module does, just join to suspend the program. The thread class in the threading module is the primary running object, and there are several common uses, such as creating a thread instance and passing it a function or a callable class object (which can be implemented by apply () to __call__ ()). or derive a subclass from thread (override run ()) and create an instance of the subclass. Here's a simple example of creating two threads, passing functions in thread instances, sleep 6, 3, and the main thread shows the total elapsed time.

#!/usr/bin/env python

import threading
from time import sleep, ctime

loops = [6, 3] # sleep sec

def Loop (n Loop, nsec):
    print ' Start loop ', Nloop, ' at: ', CTime (
    nsec)
    print ' Loop ', Nloop, ' do at: ', CTime () 
  def Main ():
    print ' starting at: ', CTime ()
    threads = []
    nloops = range (len (loops)) for

    I in nloops:
  t = Threading. Thread (target = loop, args = (i, loops[i]))
        Threads.append (t) for

    I in Nloops:
        threads[i].start ()

    For i in Nloops:
        threads[i].join ()

    print "All Done" at: ', CTime ()

if __name__ = = ' __main__ ':
    Main ()

The following are the results 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 a longer time for the child thread, 6 seconds.

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.