Python Road 46-Multi-process

Source: Internet
Author: User

Basic use of multiple processes

Import Multiprocessingimport Osimport timedef run (): print ("Parent process:%s, child process:%s"% (Os.getppid (), Os.getpid ())) Time.sleep (2) If __name__ = = "__main__": P = multiprocessing. Process (Target=run) P.start () P.join ()


Inter-process communication

Different process memory is not shared, in order to achieve data exchange between two processes, you can use the following methods


Queue

Import Multiprocessingdef F (q): Q.put (11111) If __name__ = = "__main__": q = multiprocessing. Queue () p = multiprocessing. Process (Target=f, args= (Q,)) P.start () print (Q.get ())


Pipe

Import Multiprocessingdef F (conn): Conn.send (1) conn.send (2) print (CONN.RECV ()) Conn.close () if __name__ = = "__ Main__ ": parent_conn, Child_conn = multiprocessing. Pipe () p = multiprocessing. Process (Target=f, args= (Child_conn,)) P.start () print (PARENT_CONN.RECV ()) print (PARENT_CONN.RECV ()) Parent_con N.send (3) P.join ()


Data sharing between processes

Manager

Import Multiprocessingimport osdef func (D, L): d[os.getpid ()] = Os.getpid () print (d) l.append (Os.getpid ()) PRI NT (l) if __name__ = = "__main__": Manager = multiprocessing. Manager () d = manager.dict () L = manager.list () p_list = [] for I in range (5): p = multiprocessing. Process (Target=func, args= (d, L)) P.start () P_list.append (p) for p in P_list:p.join ()


Process Lock

Lock can avoid access violations when multiple processes are accessing shared resources

Import Multiprocessingdef F (L, I): L.acquire () print ("Hello World", i) l.release () if __name__ = = "__main__": l Ock = multiprocessing. Lock () for NUM in range: p = multiprocessing. Process (Target=f, args= (lock, num)) P.start ()


Process Pool

The process pool maintains a process queue internally, and when used, gets a process in the process pool, and if there are no processes available in the process pool, the program waits until there are processes in the process pool

Import multiprocessingimport osimport timedef foo (i): Time.sleep (2) print ("In Process", os.getpid ()) return i + 1 00def Bar (ARG): print ("==>exec done:", arg) if __name__ = = "__main__": Pool = multiprocessing.    Pool (5) for I in range: Pool.apply_async (Func=foo, args= (i,), Callback=bar) print ("End") Pool.close () Pool.join ()


This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1908709

Python Road 46-Multi-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.