processes, Threads
Http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html
Multithreaded programming with threading modules [overview]
Python's Explanatory language also has a dedicated threading model, and Python virtual machines use the Gil (global interpreter lock, Globe interpreter Lock) to mutually exclusive threads access to shared resources, but are temporarily unable to take advantage of multiprocessor Benefits.
In Python we are mainly through the thread and threading these two modules, wherein the Python threading module is to do some packaging thread, can be more convenient to use, so we use The threading module implements multithreaded Programming. This article mainly looks at Python's support for multithreaded Programming.
At the language level, Python provides very good support for multithreading, allowing you to easily support the creation of threads, mutexes, semaphores, and Synchronization. The following is the official online Introduction threading Module basic information and functions:
Implementation module
Thread: multithreading of the underlying support module, generally not recommended to use;
Threading: thread is encapsulated, and some threads are Manipulated.
Threading Module
Thread threading class, This is the most we use a class, you can specify the thread function execution or inherit from it can implement sub-threading function;
A timer is similar to a thread, but waits for a period of time before it starts to run;
Lock the primitive, which we can use when the global variable is mutually exclusive;
Rlock can re-enter the lock, so that the single-threaded can obtain the acquired lock again;
Condition A conditional variable that allows a thread to stop waiting for another thread to satisfy a certain "condition";
The condition variable that is common to the Event. Multiple threads can wait for an event to occur, and all threads are activated after the event occurs;
Semaphore provides a structure similar to the "waiting room" for the thread that waits for the lock;
Boundedsemaphore is similar to semaphore, but is not allowed to exceed the initial value;
Queue: implements Multi-producer (Producer), Multi-consumer (Consumer) queues, supports lock primitives, and provides good synchronization support across multiple THREADS.
where the thread class
Is your primary threading class and you can create process Instances. The functions provided by this class include:
GetName (self) Returns the name of the thread
The IsAlive (self) Boolean flag that indicates whether the thread is still running
Isdaemon (self) returns the daemon flag for the thread
The join (self, timeout=none) program hangs until the thread ends and, if a timeout is given, blocks timeout seconds
Run (self) defines the function function of the thread
Setdaemon (self, Daemonic) Sets the Thread's daemon flag to Daemonic
SetName (self, Name) Sets the name of the thread
Start (self) starts thread execution
where queue provides the class
Queue queues
Lifoqueue after in first out (lifo) queue
Priorityqueue Priority Queue
Python Threading Module
#!/usr/bin/env python#-*-coding:utf-8-*-# author:dccimport threadingimport timedef run (n): print ("task", n) time.sl eep (2) T1 = threading. Thread (target=run,args= ("t1",)) t2 = threading. Thread (target=run,args= ("t2",)) t1.start () t2.start () print (t1.getname ()) print (t2.getname ())
#!/usr/bin/env python#-*-coding:utf-8-*-# author:dccimport threadingimport timeclass MyThread (threading. Thread): def __init__ (self,n): Super (mythread,self). __init__ () SELF.N = n def run (self): print ( "running task", self.n) time.sleep (3) t1 = MyThread ("t1") t2 = MyThread ("t2") if __name__ = = ' __main__ ': t1.start () T2.start () print (t1.getname ()) print (t2.getname ())
Import threading import time class mythread (threading. Thread): def __init__ (self,id): threading. thread.__init__ (self) self.id = id def run (self): x = 0 time.sleep (ten) print self.id if __name__ == "__main__": t1=mythread (999) T1.start () for i in range (5): print i #执行结果0 1 2 3 4 999
When running on the machine, there is a noticeable pause between 4 and 999. Explanation: after the thread T1 start, the main thread does not wait for the threads to execute after the T1 run, but to finish the 5 cycles of printing (print to 4), and then sleep (10), the thread T1 the 999 passed in to print Out.
now, Let's add the Join () method (the Other code does not change) and see What's different, for example:
Import threading import time class mythread (threading. Thread): def __init__ (self,id): threading. thread.__init__ (self) self.id = id def run (self): x = 0 time.sleep (ten) print self.id if __name __ == "__main__": t1=mythread (999) t1.start () t1.join () for i in range (5): print i #执行结果999 0 1 2 3 4
The
2, Setdaemon () method. In the main thread a, Sub-thread B is created, and B.setdaemon () is called in the main thread a, which means that the main thread A is set to the daemon thread, and if the main thread a executes, it will not be done by pipe B. and the main thread a exits. this is the meaning of the Setdaemon method, which is essentially the opposite of join. In addition, there is a special note that must be set before the start () method call, and if not set to the daemon thread, the program will be indefinitely Suspended.
Example: The setting of a child thread ends with the end of the main thread:
#!/usr/bin/env python#-*-coding:utf-8-*-# author:dccimport threadingimport timedef run (n): print ("task", n) time.sl Eep (2) print ("thread done ...", n) start_time = time.time () t_objs = [] #存线程实例for i in range: t = threading. Thread (target=run,args= ("t-%s"% i,)) t.setdaemon (True) #把当前线程设置为守护线程 t.start () t_objs.append (t) # In order not to block the boot of a later thread, not Join here, first put in a list # print (t.getname ()) #time. sleep (2) print (threading.active_count ()) # for I in T_objs: #循环线程实例列表, Wait for all threads to execute # t.join () print (time.time ()-start_time)
Thread Lock
When the CPU executes a task, it is randomly dispatched between threads, and each thread may execute a different thread instead of just n Code. Because of the sharing of resources and data between multiple threads in a process, which can easily result in resource looting or dirty data, there is a lock concept that restricts only one thread at a time from accessing a specified Data.
No yoke
Import Threadingimport timenum = 0def show (): global num num + = 1 name = T.getname () time.sleep (1) # Note that The position of this line of statement is important, and you must not observe the dirty data after NUM has been modified. Print (name, "after execution, the value of num is:", num) for i in range: t = threading. Thread (target=show) t.start () print (' main thread Stop ')
Lock locks
Ordinary locks, also known as mutexes, are exclusive and only one thread is released at the same time.
Import Timeimport threadingnum = 10def func (lock): global NUM lock.acquire () # Let the lock begin to function NUM-= 1 time.sleep (1) Print (NUM) lock.release () # release lock = Threading. Lock () # Instantiates a lock object for i in range: t = threading. Thread (target=func, args= (lock)) # Remember to pass the lock as a parameter to the func parameter T.start ()
Rlock (recursive Lock)
It's a big lock, and it's going to contain a sub-lock.
The lock class of the threading module, which does not support nested locks. The usage of the Rlcok class is identical to lock, but it supports nesting, so we generally use the Rlcok class Directly.
Import threading, timedef run1 (): print ("grab the first Part data ") lock.acquire () global num num += 1 lock.release () return numdef run2 (): print ("grab the second part data") lock.acquire () global num2 num2 += 1 lock.release () return num2def run3 (): lock.acquire () res = run1 () print ('-------- Between run1 and run2-----') res2 = run2 () lock.release () print (res, res2) if __name__ == ' __main__ ': num, num2 = 0, 0 lock = threading. Rlock () for i in range (ten): t = threading. Thread (target=run3) t.start () while threading.active_count ( ) != 1: print (threading.active_count ()) else: print ('-- --all threads done---') print (num, num2)
Time Device (timer)
Timer that specifies n seconds after an operation is Performed. Very simple but very useful thing.
From threading import timerdef Hello (): print ("hello, World") t = Timer (1, Hello) # indicates after 1 seconds to execute the Hello function t.start ()
Signal Volume (Semaphore)
This lock allows a certain number of threads to change the data at the same time, which is not a mutex. For example, subway security, queuing A lot of people, staff only allow a certain number of people into the security area, others continue to line up.
mutexes allow only one thread to change data at the same time, while Semaphore allows a certain number of threads to change data, such as a toilet with 3 pits, which allows up to 3 people to go to the toilet, while the latter can only wait for someone to come Out.
Import timeimport Threading def run (n): semaphore.acquire () print ("run the thread:%s"% n) time.sleep (1) Sema Phore.release () num = 0semaphore = Threading. Boundedsemaphore (5) # allows up to 5 threads to run at the same time for i in range: t = threading. Thread (target=run, args= (i,)) t.start ()
Events (event)
The event mainly provides three methods set, wait, clear.
Event Mechanism: The global definition of a "flag", if the value of "flag" is false, then when the program executes the wait method is blocked, if the "flag" value is true, then the wait method is no longer blocked. This kind of lock, similar to traffic light (by default is red), it is at the time of the red light Block all the threads at once, at the time of the green, all the queued threads are released at Once.
Clear: set "Flag" to False
Set: sets "Flag" to True
!/usr/bin/env python#-*- coding:utf-8 -*-# author:dccimport timeimport Threadingimport randomevent = threading. Event () def lighter (): count = 0 event.set () #先设置成绿灯 while true: if count > 5 and count < 10: #改成红灯 event.clear () #把标志位清除 print ("\033[41;1m red.....\033[0m ") elif count > 10: event.set () #设置成路灯 count = 0 else: Print ("\033[42;1m green \033[0m") time.sleep (1) count +=1def car (name): while True: if event.is_set (): #代表绿灯 print ("[%s] is running " % name ) time.sleep (2) else: print ("[% s] is waitting....... " % name) event.wait () print ("[ %s] green&Nbsp;light is on ,start going " % name) Light = threading. Thread (target=lighter,) Light.start () car1 = threading. Thread (target=car,args= ("Tesla",)) Car1.start ()
Queue
In general, a queue is a first-in-one data structure that corresponds to a last-in-first-out structure of the STACK. But in python, it has a queue module built into it that not only provides a normal queue, but also provides some special queues
Queue: FIFO queue
Import Queueq = Queue. Queue (5) q.put (q.put) q.put (+) print (q.get ()) print (q.get ()) print (q.get ())
Parameters and methods of the queue Class:
Qsize () Gets the number of elements in the current queue, that is, the size of the queue
Empty () to determine whether the current queue is empty, returns TRUE or False
Full () Determines whether the current queue is filled, returns TRUE or False
Put (self, block=true, timeout=none)
Get (self, block=true, timeout=none)
Lifoqueue: LIFO Queue
Import Queueq = Queue. Lifoqueue () q.put (123) q.put (456) print (q.get ())
Priorityqueue: Priority Queue
Q = Queue. Priorityqueue () q.put ((1, "alex1")) q.put ((1, "alex2")) q.put ((1, "alex3")) q.put ((3, "alex3")) print (q.get ())
Producer Consumer Model
Import time,randomimport queue,threadingq = queue. Queue () Def producer (name): count = 0 while count <20: time.sleep (random.randrange (3)) q.put (count) Print (' producer %s has produced %s baozi.. ') % (name, count)) count +=1def consumer (name): count = 0 while count <20: time.sleep (random.randrange (4)) if not q.empty (): data = q.get () print (data) print (' \033[32;1mconsumer %s has eat %s baozi...\033[0m ' % (name, data)) else: print ("-----NO&NBsp;baozi anymore----") count +=1p1 = threading. Thread (target=producer, args= (' A ',)) c1 = threading. Thread (target=consumer, args= (' B ',)) p1.start () c1.start ()
This article from "dongfeng ten Li tenderness" blog, please be sure to keep this source http://xiaofengcanyue.blog.51cto.com/6671161/1855621
day9---multi-threaded, Thread lock, Queue