In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Python how to use threading to achieve multithreading, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Threading is used to provide thread-related operations, and threads are the smallest unit of work in an application. The current version of the python multithread library does not implement priorities, thread groups, and threads cannot be stopped, paused, resumed, or 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 containing the running thread. Running refers to threads after starting and ending, excluding threads before starting and terminating.
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 timeout.
Thread class
Thread is a thread class, and there are two ways to use it, either directly passing in the method to run or inheriting from Thread and overriding run ():
# coding:utf-8import threadingimport time# method 1: constructor 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 endpoints method # method 2: inherit from Thread And override run () class MyThread (threading.Thread): def _ _ init__ (self,arg): super (MyThread, self). _ _ init__ () # Note: be sure to explicitly call the initialization function of the parent class. Self.arg=arg def run (self): # define the function time.sleep (1) print 'the arg is:%s\ r'% self.argfor i in xrange (4): t = MyThread (I) t.start () print 'main thread endpoints'
Construction method:
Thread (group=None, target=None, name=None, args= (), kwargs= {})
Group: thread group, which has not been implemented yet. The prompt in the library reference must be None.
Target: the method to execute
Name: thread name
Args/kwargs: the parameter of the method to pass in.
Example method:
IsAlive (): returns whether the thread is running. Running refers to after startup and before termination.
Get/setName (name): gets / sets the thread name.
Start (): thread is ready to wait for CPU scheduling
Is/setDaemon (bool): get / set is the background thread (default foreground thread (False)). (set before start)
If it is a background thread, during the execution of the main thread, the background thread is also in progress. After the execution of the main thread, both the main thread and the background thread stop regardless of whether the background thread is successful or not.
If it is a foreground thread, during the execution of the main thread, the foreground thread is also in progress. After the execution of the main thread is completed, the program stops after waiting for the foreground thread to complete its execution.
Start (): starts the thread.
Join ([timeout]): blocks the thread in the current context until the thread calling this method terminates or reaches the specified timeout (optional parameter).
Use example 1 (setDeamon is not set):
# coding:utf-8import threadingimport timedef action (arg): time.sleep (1) print 'sub thread startling the thread name is:%s\ r'% threading.currentThread (). GetName () print' the arg is:%s\ r'% arg time.sleep (1) for i in xrange (4): t = threading.Thread (target=action,args= (I) ) t.start () print 'main_thread enduring mainlines thread enduring sub thread starting the thread name is:Thread-2the arg is:1the arg is:0sub thread startling the thread name is:Thread-4the arg is:2the arg is:3Process finished with exit code 0 can be seen For the four "foreground" threads created, the foreground thread is also in progress during the execution of the main thread. After the execution of the main thread is completed, the program stops after waiting for the foreground thread to complete its execution.
The serDeamon (False) (default) foreground thread is verified. During the execution of the main thread, the foreground thread is also in progress. After the execution of the main thread is completed, after waiting for the foreground thread to complete, the main thread stops.
Use example 2 (setDeamon=True)
# coding:utf-8import threadingimport timedef action (arg): time.sleep (1) print 'sub thread startling the thread name is:%s\ r'% threading.currentThread (). GetName () print' the arg is:%s\ r'% arg time.sleep (1) for i in xrange (4): t = threading.Thread (target=action,args= (I) ) t.setDaemon (True) # set the thread as the background thread t.start () print 'main_thread endgame mainlines thread endgame process finished with exit code 0 can see After the execution of the main thread, the background thread stops regardless of whether it is successful or not.
Verified the serDeamon (True) background thread, the main thread execution process, the background thread is also in progress, after the main thread execution, whether the background thread is successful or not, the main thread stops.
Use example 3 (set up join)
# coding:utf-8import threadingimport timedef action (arg): time.sleep (1) print 'sub thread starting the thread name is:%s'% threading.currentThread (). GetName () print 'the arg is:%s'% arg time.sleep (1) thread_list = [] # Thread Storage 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 starting the thread name is:Thread-2 the arg is:1 sub thread starting the thread name is:Thread-3 the arg is:2 sub thread starting the thread name is:Thread-1 the arg is:0 sub thread starting the thread name is:Thread-4 the arg After is:3 main_thread endgame process finished with exit code 0 sets join The main thread ends after all the execution of the child thread is completed or the child thread times out.
It is verified that join () blocks the thread of the current context until the thread calling this method terminates or reaches the specified timeout, even if setDeamon (True) is set, the main thread still waits for the child thread to finish.
Use example 4 (improper use of join to make multithreaded programming execute sequentially)
# coding:utf-8import threadingimport timedef action (arg): time.sleep (1) print 'sub thread starting 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 enduring sub thread starting the thread name is:Thread-1 the arg is:0 sub thread starting the thread name is:Thread-2 the arg is:1 sub thread starting the thread name is:Thread-3 the arg is:2 sub thread starting the thread name is:Thread-4 the arg is:3 main_thread enduring process finished with exit code 0 can see that at this time Programs can only be executed sequentially, and each thread is blocked by the join of the previous thread, making "multithreading" lose the meaning of multithreading.
Lock, Rock class
Due to random scheduling between threads: after one thread executes n, CPU then executes other threads. In order to avoid confusion when multiple threads operate on a resource in memory at the same time, we use locks.
Lock (instruction lock) is the lowest level of synchronization instruction available. When the Lock is locked, it is not owned by a specific thread. Lock contains two states-locked and unlocked, as well as two basic methods.
You can assume that Lock has a lock pool, and when a thread requests a lock, it places the thread in the pool until the lock is acquired and 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 synchronization instruction that can be requested multiple times by the same thread. RLock uses the concepts of "owned thread" and "recursive level". When it is locked, the RLock is owned by a thread. The thread that owns RLock can call acquire () again, and release () needs to be called the same number of times when the lock is released.
It can be thought that RLock contains a lock pool and a counter with an initial value of 0. Every time acquire () / release () is called successfully, the counter will be + 1. When 0, the lock is unlocked.
In short: Lock belongs to the global, Rlock belongs to the thread.
Construction method:
Lock (), Rlock (), Rlock () is recommended
Example method:
Acquire ([timeout]): try to get a lock. Put the thread into a synchronous blocking state.
Release (): releases the lock. The thread must have acquired the lock before use, or an exception will be thrown.
Example 1 (lock not 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 (10): t = threading.Thread (target=show, args= (I,)) t.start () print 'main thread stop'main thread stop12 34568 9910Process finished with exit code 0 may cause confusion. This kind of scenario is suitable for using locks.
Example 2 (using locks):
# coding:utf-8import threadingimport timegl_num = 0lock = threading.RLock () # when calling acquire ([timeout]), the thread blocks until # gets the lock or until timeout seconds later (the timeout parameter is optional). # returns whether to acquire the lock. Def Func (): lock.acquire () global gl_num gl_num + = 1 time.sleep (1) print gl_num lock.release () for i in range (10): t = threading.Thread (target=Func) t.start () 12345678910Process finished with exit code 0 shows that the global variable must acquire a lock every time it is called before it can operate, thus ensuring the security of the shared data.
Lock vs. Rlock
# coding:utf-8import threadinglock = threading.Lock () # Lock object lock.acquire () lock.acquire () # resulted in a deadlock Lock.release () lock.release () print lock.acquire () import threadingrLock = threading.RLock () # RLock object rLock.acquire () rLock.acquire () # within the same thread, the program will not block. RLock.release () rLock.release ()
Condition class
A Condition (condition variable) is usually associated with a lock. When you need to share a lock among multiple Contidion, you can pass a Lock/RLock instance to the constructor, otherwise it will generate an RLock instance of its own.
It can be assumed that, in addition to the lock pool that comes with Lock, Condition contains a waiting pool where threads are waiting to block until another thread calls notify () / notifyAll () notification; after being notified, the thread enters the lock pool and waits for a lock.
Construction method:
Condition ([lock/rlock])
Example method:
Acquire ([timeout]) / release (): invokes the corresponding method of the associated lock.
Wait ([timeout]): calling this method causes the thread to enter the Condition's waiting pool to wait for notification and release the lock. The thread must have acquired the lock before use, or an exception will be thrown.
Notify (): calling this method will pick a thread from the waiting pool and notify that the thread receiving the notification will automatically call acquire () to attempt to acquire the lock (enter the lock pool); other threads are still in the waiting pool. Calling this method does not release the lock. The thread must have acquired the lock before use, or an exception will be thrown.
NotifyAll (): calling this method will notify all threads in the waiting pool that they will enter the lock pool and try to acquire the lock. Calling this method does not release the lock. The thread must have acquired the lock before use, or an exception will be thrown.
Example 1: producer-consumer model
# encoding: UTF-8import threadingimport time# goods 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' # notify consumers The product has been produced con.notify () # waiting for notification con.wait () time.sleep (2) # Consumer method def consume (): global product if con.acquire (): while True: if product is not None: print 'consume...' Product = None # Notification producer Con.notify () # waiting 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... The Process finished with exit code-1 program runs in a loop. Repeat the process of production and consumption.
Example 2: producer-consumer model
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):deliver one, now products:%s" %(self.name, products) condition.notify()#不释放锁定,因此需要下面一句 condition.release() else: print "Producer(%s):already 10, stop deliver, now products:%s" %(self.name, products) condition.wait();#自动释放锁定 time.sleep(2)class 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"% (self.name, products) condition.notify () condition.release () else: print "Consumer (% s): only 1, stop consume, products:%s"% (self.name 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 3:
Import threading alist = Nonecondition = threading.Condition () def doSet (): if condition.acquire (): while alist is 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 (10)] 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 (event) is one of the simplest thread communication mechanisms: one thread notifies the event and other threads wait for the event. Event has a built-in flag that starts with False, which is set to True when set () is called and reset to False when clear () is called. Wait () blocks the thread to the waiting blocking state.
Event is actually a simplified version of Condition. Event does not have a lock and cannot put the thread into 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 for blocking to resume running state.
Clear (): set the flag to False.
Wait ([timeout]): return immediately if marked as True, otherwise block the thread to wait for blocking state and wait for other threads to call set ().
Example one
# encoding: UTF-8import threadingimport timeevent = threading.Event () def func (): # wait event Enter the waiting blocking state print'% s wait for event...'% threading.currentThread (). GetName () event.wait () # enter the running state after receiving the event 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 seconds later. MainThread set event.Thread-1 recv event. Thread-2 recv event.Process finished with exit code 0
Timer class
Timer (timer) is a derived class of Thread that is used to call a method after a specified time.
Construction method:
Timer (interval, function, args= [], kwargs= {})
Interval: specified time
Function: the method to execute
Args/kwargs: parameters of the method
Example method:
Timer derives from Thread and does not add instance methods.
Example 1:
# encoding: UTF-8import threadingdef func (): print 'hello timers timer = threading.Timer (5, func) timer.start ()
The thread executes after a delay of 5 seconds.
Local class
Local is a lowercase class that manages thread-local (thread-local) data. For the same local, threads cannot access properties set by other threads; properties set by threads are not replaced by properties of the same name set by other threads.
You can think of local as a "thread-attribute dictionary". Local encapsulates the details of retrieving the corresponding attribute dictionary from its own thread as key and using the attribute name as key to retrieve attribute values.
# 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.tnamenotmainmain, is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.