Python threads, processes

Source: Internet
Author: User
Tags mutex semaphore

Process: A collection of resources

Threads: Minimum debug unit for operating CPU

The simplest examples of multithreading are:

There are 2 ways to call #!/usr/bin/python#author:sean# threads, as follows: #直接调用import  threadingimport timedef run (n):     print ("task ", N)     time.sleep (2) if __name__ ==  ' __ Main__ ':     t1 = threading. Thread (target=run,args= ("T1",))    #生成一个线程实例     t2 = threading. Thread (target=run,args= ("T2",))    #生成另一个线程实例     t1.start ()    #启动线程      t2.start ()    #启动另一个线程     print (T1.getname ())   #获取线程名      print (T2.getname ()) Run ("T1") Run ("T2") #继承式调用class  mythread (threading. Thread):     def __init__ (self,num,sleep_time):         super (mythread,self). __init__ ()         self.num =  num        self.sleep_time = sleep_time &nBsp;  def run (self):   #定义每个线程要运行的函数          Print ("running on number:%s"% self.num)          Time.sleep (self.sleep_time)         print ("Task done,", Self.num) if  __name__ ==  ' __main__ ':     t1 = mythread ("T1", 2)      t2 = mythread ("T2", 4)     t1.start ()     t2.start ( )     t1.join ()     t2.join ()     print ("The  Main ")     print (T1.getname ())     print (T2.getname ())

There is no comparability between process and thread execution speed

Process contains at least one thread

Threads and processes in Python are native threads and processes that are used by the operating system

Native processes and native threads are maintained and managed by the operating system

Multithreading in Python is pseudo-multi-threading, in fact only one thread is running at the same time

Multithreading in Python is essentially a form of performance in which the context is constantly switched in a single thread

Io operation does not consume CPU, such as reading data from hard disk

Calculate CPU usage, such as

When will the use of pseudo-multithreading? How to improve efficiency?

Python multithreading is not suitable for CPU-intensive operation tasks

Python multithreading is suitable for IO-intensive tasks such as sockets

Inter-thread memory is shared

The thread must lock (mutex mutex) when modifying the same data at the same time

Recursive lock: Lock in Locks, enter 2 door example

Join: Wait for a thread to finish executing

def run (n): print ("Run thread ...") T = Threading. Thread (target=run,args= (n,)) T.start () T.join ()

Semaphore: Allows multiple threads to operate at the same time

Daemon Thread: Service to non-daemon thread (Emperor's relationship with Eunuch, Emperor died, courtiers to immolation)

The role of the queue:

Decoupling of the implementation program (allows for loosely coupling between programs)

Improve processing efficiency

#!/usr/bin/python#author:seanimport queue# FIFO mode FIFOq1 = queue. Queue () q1.put (' D1 ') q1.put (' D2 ') q1.put (' D3 ') print (Q1.qsize ()) Q1.get () Q1.get () print (Q1.qsize ()) Print (q1.get_ NoWait ()) q1.get_nowait () #后进先出方式 (example of selling fruit) q2 = queue. Lifoqueue () #Lifo即Last in first outq2.put (' D1 ') q2.put (' D2 ') q2.put (' D3 ') print (Q2.qsize ()) Q2.get () Q2.get () print ( Q2.qsize ()) print (q2.get_nowait ()) q2.get_nowait () #按优先级来处理的方式q3 = queue. Priorityqueue () #存储数据时可设置优先级的队列q3. Put (( -1, "haha")) Q3.put ((5, "Tom")) Q3.put ((+, "Sean")) Q3.put ((0, "Jerry")) print ( Q3.get ()) print (Q3.get ()) print (Q3.get ()) print (Q3.get ())

List differs from queue:

Taking a data from a list is equivalent to copying the data from a list, and the metadata in the list is not changed

Take a data from the queue, and the data is not available in the queue after it's taken.

Producer Consumer Model:

#!/usr/bin/python#author:seanimport threadingimport timeimport queueq = queue. Queue (maxsize=10) def producer (name):    count = 1     While true:        q.put ("Bone%s"  % count)          print ("Production of Bones", count)         count  += 1        time.sleep (0.1) Def  consumer (name):      #while  q.qsize () >0:    while True:         print ("[%s]  takes to [%s]  and eats it ..."  % (Name, q.get ()))          time.sleep (1) p = threading. Thread (target=producer,args= ("Sean",)) c = threading. Thread (target=consumer,args= ("Jason",)) c1 = threading. Thread (target=consumer,args= ("Xiao Wang",)) C2 =&nbsP;threading. Thread (target=consumer,args= ("Jerry",)) c3 = threading. Thread (target=consumer,args= ("Tom",)) P.start () C.start () C1.start () C2.start () C3.start ()

Events: Event

Example of traffic lights:

#!/usr/bin/python#author:seanimport timeimport threadingevent = threading. Event () Def lighter ():     count = 0    event.set ()   #先设为绿灯     while true:        if count  > 5 and count < 10:   #改成红灯              event.clear ()     #把标志位清空              print ("\033[41;1mred light is on...\033[0m")          elif count > 10:             event.set ()   #变绿灯              count = 0        else:          &nBsp;  print ("\033[42;1mgreen light is on...\033[0m")          time.sleep (1)         count += 1def  Car (name):    while true:        if  Event.is_set ():   #代表当前是绿灯              Print ("[%s] running ..."% name)              time.sleep (1)         else:             print ("[%s] sees red light,waiting ..."% name)              event.wait ()              print ("\033[34;1m[%s] green light is on,start  going...\033[0m "% name) liGht = threading. Thread (Target=lighter,) Light.start () car1 = threading. Thread (target=car,args= (' Tesla ',)) Car1.start ()

Multi-process in Python:

Processes are independent and memory is exclusive

Multi-process Example:

#!/usr/bin/python#author:seanimport multiprocessingimport timedef F (name): Time.sleep (2) print ("Hello", name) if __nam e__ = = ' __main__ ': For I in range: P = multiprocessing. Process (target=f,args= (' Bob%s '% i,)) P.start () # P.join ()

Get process ID:

#!/usr/bin/python#author:seanfrom multiprocessing Import processimport osdef info (title): Print (' module ' Name: ', __name__) print (' Parent process: ', os.getppid ()) print (' Process ID: ', os.getpid ()) print ("\ n") def f (NA    Me): info (' \033[31;1mcalled from child process function f\033[0m ') print (' Hello ', name) if __name__ = = ' __main__ ': Info (' \033[32;1mmain process line\033[0m ') p = Process (target=f, args= (' Bob ',)) P.start () # P.join ()

In Python, each child process is started by its parent process.

Inter-process communication:

Memory between different processes is not shared, and to achieve data exchange between two processes, you can use the following methods:

1, Queues

Use the same way as a queue in threading.

#!/usr/bin/python#author:seanfrom multiprocessing Import process,queuedef F (q): Q.put ([42,none, ' hello ']) if __name__ = = ' __main__ ': q = Queue () p = Process (target=f,args= (q,)) P.start () print (Q.get ()) #prints "[42,none, ' hello '] "P.join ()

2, Pipes

The pipe () function returns a pair of connection objects connected by a Pipe which by default is duplex (two-way). For example:

#!/usr/bin/python#author:seanfrom multiprocessing Import process,pipedef F (conn): Conn.send ([42,none, ' Hello from Child ']) conn.close () if __name__ = = ' __main__ ': Parent_conn,child_conn = Pipe () p = Process (target=f,args= (child_ conn,)) P.start () print (PARENT_CONN.RECV ()) #prints "[42,none, ' hello ']" p.join ()

The above-mentioned queues and pipes just implement the data exchange between processes, and do not implement memory sharing between processes, to achieve memory sharing between processes, you use the Managers

3, Managers

A Manager Object RETURND by manager () controls a server process which holds Python objects and allows other processes to M Anipulate them using proxies.

A Manager returned by manager () would support following types:

List,dict,namespace,lock,rlock,semaphore,boundedsemaphore,condition,event,barrier,queue,value and Array.For Example

#!/usr/bin/python#author:seanfrom multiprocessing import process, managerimport osdef  f (d, l):    d[1] =  ' 1 '     d[' 2 '] = 2     d["pid%s"  %os.getpid ()] = os.getpid ()     l.append (1 )     print (l,d) if __name__ ==  ' __main__ ':    with  Manager ()  as manager:        d = manager.dict ()         l = manager.list (Range (5))          p_list = []        for i  In range (Ten):            p =  Process (target=f, args= (d, l))              p.start () &NBSP;&NBSP;&NBsp;         p_list.append (P)          for res in p_list:             res.join ()         l.append ("From parent")          print (d)         print (L)
#!/usr/bin/python#author:seanfrom multiprocessing import process, managerimport osdef  f (d, l):     d[os.getpid ()] =os.getpid ()     l.append ( Os.getpid ())     print (L) if __name__ ==  ' __main__ ':     With manager ()  as manager:        d =  Manager.dict ()  #{}  #生成一个字典, which can be shared and delivered across multiple processes         l =  manager.list (Range (5)) #生成一个列表 that can be shared and delivered across multiple processes         p_list =  []        for i in range (Ten):             p = process (target=f, args= (d, l))             p.start ()               p_list.append (p)         for res in p_list:   #等待结果             res.join ()          print (d)         print (L)

Process synchronization

The without using the lock output from the different processes are liable to get all mixed up.

#!/usr/bin/python#author:seanfrom multiprocessing Import process,lockdef F (l,i): L.acquire () try:print (' Hell O World ', i) finally:l.release () if __name__ = = ' __main__ ': Lock = lock () for NUM in range: Proc ESS (target=f,args= (Lock,num)). Start ()

Process Pool

A process sequence is maintained internally by the process pool, and when used, a process is fetched in the process pool, and if no process is available in the process pool sequence, the program waits until a process is available in the process pool.

There are two methods of process pooling:

1. Apply

2, Apply_async






This article is from the "Home" blog, please make sure to keep this source http://itchentao.blog.51cto.com/5168625/1894874

Python threads, processes

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.