Python Multithreading Summary

Source: Internet
Author: User
Tags instance method terminates thread stop

threading is used to provide thread-related operations, which are the smallest unit of work in an application. Python's current version of multiline libraries does not implement priority, thread groups, and threads cannot be stopped, paused, resumed, interrupted.

Classes provided by the threading module:
Thread, Lock, Rlock, Condition, [Bounded]semaphore, Event, Timer, Local.

common methods provided by the threading module:
Threading.currentthread (): Returns the current thread variable.
Threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends.
Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).

Constants provided by the threading module:

Threading. Timeout_max sets the threading global time-out.

Thread class

Thread is a threading class, and there are two ways of using it, passing directly to the method to be run or inheriting from thread and overwriting run ():

# Coding:utf-8import Threadingimport time# Method One: The method to be executed is passed as a parameter to thread's construction method def action (ARG):    time.sleep (1)    print ' The arg is:%s\r '%argfor i in Xrange (4):    t =threading. Thread (target=action,args= (i,))    T.start () print ' main thread end! ' #方法二: Inherit from thread and override run () class MyThread (threading. Thread):    def __init__ (self,arg):        super (MyThread, self). __init__ () #注意: Be sure to call the initialization function of the parent class explicitly.        Self.arg=arg    def run (self): #定义每个线程要运行的函数        time.sleep (1)        print ' The arg is:%s\r '% self.argfor i in Xrange (4):    t =mythread (i)    t.start () print ' main thread end! '

Construction Method:
Thread (Group=none, Target=none, Name=none, args= (), kwargs={})

Group: Thread groups, currently not implemented, the library reference must be none;
Target: The method to be executed;
Name: thread name;
Args/kwargs: The parameter to pass in the method.

Example method:
IsAlive (): Returns whether the thread is running. Running refers to after startup, before terminating.
Get/setname (name): Gets/sets the thread name.

Start (): Thread ready to wait for CPU scheduling
Is/setdaemon (BOOL): Gets/sets is a background thread (the default foreground thread (False)). (set before Start)

If it is a background thread, during the main thread execution, the background thread is also in progress, and after the main thread executes, the main thread and the background thread will stop, regardless of the success or not.
If it is the foreground thread, during the main thread execution, the foreground thread is also in progress, and after the main thread finishes executing, wait for the foreground thread to finish, the program stops
Start (): Starts the thread.
Join ([timeout]): Blocks the thread of the current context until the thread calling this method terminates or arrives at the specified timeout (optional parameter).

Use example one (Setdeamon not set):

# Coding:utf-8import Threadingimport timedef Action (ARG):    time.sleep (1)    print  ' sub thread start!the Thread name is:%s\r '% threading.currentthread (). GetName ()    print ' The arg is:%s\r '%arg    time.sleep (1) for I in Xra Nge (4):    t =threading. Thread (target=action,args= (i,))    T.start () print ' Main_thread end! '

Main_thread end!sub thread start!the thread name is:thread-2the arg is:1the arg is:0sub thread start!the thread name is:th read-4the Arg is:2the arg is:3process finished with exit code 0 can be seen, the creation of 4 "foreground" threads, the main thread execution process, the foreground thread is also in progress, after the main thread executes, wait for the foreground thread to also perform after the completion , the program stops

The Serdeamon (False) (default) foreground thread is validated, and the foreground thread is also in progress during the main thread execution, and the main thread stops after the main thread finishes executing and waits for the foreground thread to finish executing.

Use Example two (setdeamon=true)

# Coding:utf-8import Threadingimport timedef Action (ARG):    time.sleep (1)    print  ' sub thread start!the Thread name is:%s\r '% threading.currentthread (). GetName ()    print ' The arg is:%s\r '%arg    time.sleep (1) for I in Xra Nge (4):    t =threading. Thread (target=action,args= (i,))    T.setdaemon (True) #设置线程为后台线程    t.start () print ' Main_thread end! '
Run Results

The Serdeamon (True) Background thread is validated, the background thread is also in progress during the main thread execution, and after the main thread executes, the main thread is stopped regardless of the success or not.

Use example three (set join)

#coding: Utf-8import threadingimport timedef Action (ARG):    time.sleep (1)    print  ' sub thread start!the Thread name is:%s    '% threading.currentthread (). GetName ()    print ' The arg is:%s   '%arg    time.sleep (1) Thread_list = []    #线程存放列表for i in Xrange (4):    t =threading. Thread (target=action,args= (i,))    T.setdaemon (True)    thread_list.append (t) for T in Thread_list:    T.start () for T in Thread_list:    t.join ()
Sub thread start!the thread name is:thread-2 the    arg is:1   sub thread start!the thread name is:thread-3    the Arg is:2   sub thread start!the thread name is:thread-1 the    arg is:0   sub thread start!the thread name Is:thre Ad-4 the    arg is:3   main_thread end! Process finished with exit code 0 after a join is set, the main thread waits for the child thread to complete after all or the blocks until those of the child line is completed.

Verifies that join () blocks the thread of the current context until the thread that called the method terminates or arrives at the specified timeout, even if the Setdeamon (True) main thread is set to wait for the child thread to end.

Use example four (join inappropriate usage, make multithreaded programming sequence execution)

#coding: Utf-8import threadingimport timedef Action (ARG):    time.sleep (1)    print  ' sub thread start!the Thread name is:%s    '% threading.currentthread (). GetName ()    print ' The arg is:%s   '%arg    time.sleep (1) for I in Xrange (4):    t =threading. Thread (target=action,args= (i,))    T.setdaemon (True)    T.start ()    t.join () print ' Main_thread end! '
Sub thread start!the thread name is:thread-1 the    arg is:0   sub thread start!the thread name is:thread-2 the    arg Is:1   sub Thread start!the thread name is:thread-3 the    arg is:2   sub thread start!the thread name is:thread-4
   the arg is:3   main_thread end! Process finished with exit code 0 can be seen at this time, the program can only be executed sequentially, each thread is blocked by the join of the previous thread, so that "multithreading" lost multi-threaded meaning.

Lock, Rlock class

Due to random scheduling between threads: A thread may execute another thread after n is executed. In order for multiple threads to operate an in-memory resource at the same time without confusion, we use locks.

Lock (Command Lock) is the lowest level of synchronization instruction available. When lock is locked, it is not owned by a particular thread. Lock contains two states-locking and non-locking, and two basic methods.

You can think of lock as having a lock pool, and when a thread requests a lock, the thread will be in the pool until it gets locked out of the pool. The threads in the pool are in a synchronous blocking state in the state diagram.

A rlock (Reentrant lock) is a synchronous instruction that can be requested multiple times by the same thread. Rlock uses the concept of "owned threads" and "recursive hierarchy", while in a locked state, Rlock is owned by a thread. The thread that owns the Rlock can call acquire () again, releasing the lock at the same number of times that it needs to call release ().

It can be thought that Rlock contains a lock pool and a counter with an initial value of 0, each time the acquire ()/release () is successfully called, the counter is +1/-1, and the lock is unlocked for 0 o'clock.

In short: Lock belongs to the global, Rlock belongs to the thread.

Construction Method:
Lock (), Rlock (), recommended for use with Rlock ()

Example method:
Acquire ([timeout]): Try to get a lock. Causes the thread to enter a synchronous blocking state.
Release (): Releases the lock. The use of the front thread must have been locked or an exception will be thrown.

Example one (no lock used):

#coding: utf-8import threadingimport timegl_num = 0def Show (ARG):    global Gl_num    time.sleep (1)    Gl_num +=1    Print gl_numfor i in range (Ten):    t = Threading. Thread (target=show, args= (i,))    T.start () print ' main thread stop '
Main thread Stop12 34568 9910Process finished with exit code 0 multiple runs can cause confusion. This scenario is suitable for use with locks.

Example two (using lock):

# coding:utf-8import Threadingimport timegl_num = 0lock = Threading. When Rlock () # Calls Acquire ([timeout]), the thread will block until the lock is acquired or until timeout seconds (the timeout parameter is optional). # returns whether the lock is acquired. Def Func ():    lock.acquire ()    global gl_num    gl_num + 1    time.sleep (1)    print Gl_num    Lock.release () for I in range:    t = Threading. Thread (Target=func)    T.start ()
12345678910Process finished with exit code 0, it can be seen that the global variable is locked every time it is called to operate, thus ensuring the security of shared data

Lock contrast Rlock

#coding:utf-8import threadinglock =threading.Lock() #Lock对象lock.acquire()lock.acquire()  #产生了死锁。lock.release()lock.release()printlock.acquire()importthreadingrLock =threading.RLock()  #RLock对象rLock.acquire()rLock.acquire() #在同一线程内,程序不会堵塞。rLock.release()rLock.release()

Condition class

A Condition (condition variable) is usually associated with a lock. When you need to share a lock in multiple contidion, you can pass an Lock/rlock instance to the constructor method, or it will generate itself a rlock instance.

It can be assumed that, in addition to the lock pool with lock, the condition also contains a wait pool, in which the thread in the pool waits to be blocked until another thread calls notify ()/notifyall () notification, and the thread enters the lock pool to wait for the lock after being notified.

Construction Method:
Condition ([Lock/rlock])

Example method:
Acquire ([timeout])/release (): The appropriate method for invoking the associated lock.
Wait ([timeout]): Calling this method will cause the thread to enter the condition waiting pool to wait for notification and release the lock. The use of the front thread must have been locked or an exception will be thrown.
Notify (): Calling this method picks up a thread from the wait pool and notifies that the thread that receives the notification will automatically call acquire () to attempt to obtain a lock (into the lock pool); Other threads are still waiting in the pool. Calling this method does not release the lock. The use of the front thread must have been locked or an exception will be thrown.
Notifyall (): Calling this method notifies all threads waiting in the pool that will enter the lock pool to attempt to obtain a lock. Calling this method does not release the lock. The use of the front thread must have been locked or an exception will be thrown.

Example one: Producer Consumer Models

# encoding:utf-8import Threadingimport time# product = none# condition variable con = threading. Condition () # producer Method Def produce ():    global Product    if Con.acquire (): While        True:            If product is None:                print ' Produce ... '                product = ' anything '                # notifies the consumer that the product has been produced                con.notify ()            # Waiting to be notified            con.wait ()            Time.sleep (2) # consumer method def consume ():    global Product    if Con.acquire (): While        True:            if product was not None:                print ' consume ... '                product = None                # Notifies the producer that the product has gone                con.notify ()            # Wait for notification            con.wait ()            Time.sleep (2) T1 = Threading. Thread (target=produce) t2 = Threading. Thread (Target=consume) T2.start () T1.start ()
Produce...consume...produce...consume...produce...consume...produce...consume...produce...consume ... Process finished with exit Code-1 program keeps running. Repeat the production and consumption process.

Example two: Producer Consumer models

Import Threadingimport timecondition = Threading. Condition () products = 0class Producer (threading. Thread): def run (self): Global products while True:if Condition.acquire (): If                    Products < 10:products + = 1; Print "Producer (%s):d Eliver One, now products:%s"% (self.name, Products) condition.notify () #不释放锁定, so need to Face one sentence condition.release () else:print "Producer (%s): Already, stop de Liver, now products:%s "% (self.name, Products) condition.wait (); #自动释放锁定 time.sleep (2) CLA SS Consumer (Threading. Thread): def run (self): Global products while True:if Condition.acquire (): If Products > 1:products-= 1 print "Consumer (%s): Consume one, now products:%s"% (s Elf.name, Products) condition.notify () cOndition.release () Else:print "Consumer (%s): Only 1, stop consume, products:%s"% (SELF.N                AME, Products) condition.wait (); Time.sleep (2) if __name__ = = "__main__": for P in range (0, 2): P = Producer () P.start () for C in range ( 0, 3): c = Consumer () C.start ()

Example three:

Import threading alist = Nonecondition = Threading. Condition () def doset ():    if Condition.acquire (): While        alist was None:            condition.wait () for        I in range ( Len (alist)) [:: -1]:            alist[i] = 1        condition.release () def doprint ():    if Condition.acquire ():        while Alist is None:            condition.wait ()        for i in alist:            print I,        print        condition.release () def Docreate ():    global alist    if Condition.acquire ():        if Alist is None:            alist = [0 for i in range]]
   condition.notifyall ()        condition.release () Tset = Threading. Thread (target=doset,name= ' tset ') Tprint = threading. Thread (target=doprint,name= ' tprint ') Tcreate = threading. Thread (target=docreate,name= ' tcreate ') Tset.start () Tprint.start () Tcreate.start ()

Event class

Event is one of the simplest threading mechanisms: one thread notifies the event, and the other thread waits for the event. The event contains a flag that is initially false, which is set to True when set () is called and reset to False when the call to clear () is called. Wait () blocks the thread until it waits for a blocking state.

Event is actually a simplified version of the Condition. The event has no lock and cannot cause the thread to enter a synchronous blocking state.

Construction Method:
Event ()

Example method:
IsSet (): Returns True when the built-in flag is true.
Set (): Sets the flag to true and notifies all threads waiting to be blocked to resume running state.
Clear (): Sets the flag to false.
Wait ([timeout]): If the flag is true will return immediately, otherwise block the thread to wait for the blocking state, waiting for other threads to call set ().

Example One

# encoding:utf-8import Threadingimport timeevent = Threading. Event () def func ():    # waits for event, enters wait blocking state    print '%s wait for event ... '% threading.currentthread (). GetName ()    Event.wait ()    # received an event after entering the running status of    print '%s recv event. '% Threading.currentthread (). GetName () T1 = Threading. Thread (target=func) t2 = Threading. Thread (Target=func) T1.start () T2.start () Time.sleep (2) # Send event notification print ' Mainthread set event. ' Event.set ()

Thread-1 Wait for event ... Thread-2 wait for event ... #2秒后 ... Mainthread Set event. Thread-1 recv event. Thread-2 recv event. Process finished with exit code 0

Timer class

A timer (timer) is a derived class of thread that is used to invoke a method after a specified time.

Construction Method:
Timer (interval, function, args=[], kwargs={})
Interval: The specified time
Function: The method to be executed
Args/kwargs: Parameters of the method

Example method:
The timer derives from thread without adding an instance method.

Example one:

# Encoding:utf-8import Threadingdef func ():    print ' Hello timer! ' Timer = Threading. Timer (5, func) Timer.start ()

The thread is deferred for 5 seconds after execution.

Local class

Local is a class that starts with a lowercase letter and is used to manage thread-local (thread-local) data. For the same local, the thread cannot access the properties set by other threads, and the properties set by the thread are not replaced with properties of the same name set by other threads.

You can think of local as a "thread-Property Dictionary" dictionary, and the local package retrieves the corresponding property dictionary from its own use thread as key, and then uses the property name as key to retrieve the property value.

# encoding:utf-8import Threading local = threading.local () local.tname = ' main ' def func ():    local.tname = ' Notmain ' 
   print local.tname t1 = Threading. Thread (Target=func) T1.start () t1.join () print Local.tname
Notmainmain

Python Multithreading Summary

Related Article

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.