Conquer the python3-process

Source: Internet
Author: User
Tags for in range

---restore content starts---

Process

Process is a process of execution of a program, is a dynamic concept, is the smallest unit of operating system scheduling.

Multiprocessing class

The process class is used to describe a processing object. When you create a subprocess, you only need to pass in a parameter that executes functions and functions to complete the process sample creation.

The Star () method starts the process

  join() 方法实现进程间的同步,Wait for all processes to exit.

Close () is used to prevent unwanted processes from flooding the process pool to cause process blocking.

Multiprocessing. Process (Group=none, Target=none, Name=none, args= (), kwargs={}, *, Daemon=none)

Target is the function name, the function that needs to be called

The parameters required by the args function,以 tuple 的形式传入

Create Child process

Call directly

 fromMultiprocessingImportProcessImport Timedeff (name): Time.sleep (2)    Print('Hello', name)if __name__=='__main__': P= Process (Target=f, args= ('Bob',)) P.start () P.join ( )

An inherited invocation

 fromMultiprocessingImportProcessImport Timeclassmyprocess (Process):def __init__(Self,name): Super ().__init__() Self.name=namedefRun (self):Print('task <%s> is runing'%self.name) Time.sleep (2)        Print('task <%s> is done'%self.name)if __name__=='__main__': P=myprocess ('Egon') P.start ()Print('Master')

As with threads, the Run method must override

Inter-process communication

Only affinity processes are described here to communicate.

Process queue

Thread Queue shared memory space
Process queue does not share memory space, it simply passes data to achieve the purpose of interprocess communication

 fromMultiprocessingImportProcess,queueImportThreading,queuedefFun (QQ): Qq.put ("Wurui")if __name__=='__main__': Q=Queue ()#Q=queue. Queue ()P=process (Target=fun, args=(q,)) P.start ()Print(Q.get ()) P.join ()
View Code

Pipeline Communication Pipe

 from Import Process,pipe def F (conn):    conn.send ("hello! " if__name__'__main__':    p_conn,c_conn =Pipe ()    p=process (target=f,args=c_conn)    p.start ()    Print (P_conn.recv ())    P.join ()
View Code

Manager

A Manager object returned by Manager() controls a server process which holds Python objects and allows other processes to Mani Pulate them using proxies.

A Manager returned byManager()Would support typeslist,dict,Namespace,Lock,RLock,Semaphore,BoundedSemaphore,Condition,Event,Barrier,Queue,ValueandArray. For example:

 fromMultiprocessingImportProcess,managerImportOSdeff (d,l): D[os.getpid ()]=os.getpid () l.append (Os.getpid ())Print(L)if __name__=='__main__': With Manager () as M:d=m.dict () L=m.list (Range (5)) P_list=[]         forIinchRange (10): P=process (target=f,args=(d,l)) P.start () P_list.append (p) forJinchP_list:j.join ()Print(d)Print(l)
View Code

Lock (Screen Lock: Prevent one process from printing complete, another process inserting)

def f (l,i):    l.acquire ()    print("hello", i)    l.release ()  if__name__'__main__':    l=Lock ( )    for in range :        Process (target=f,args= (l,i)). Start ()
View Code

Process Pool

---restore content ends---

Conquer the python3-process

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.