Multi-Process-multiprocess

Source: Internet
Author: User

Method (similar to threading):

    • Is_alive ()

    • Join (timeout)

    • Run ()

    • Start () #开始某个进程

properties:

    • Daemon (to be set by Start () and set before)

    • ExitCode (process is none at run time, if –n, indicates end of signal N)

    • Name #线程的名称

    • PID #线程的pid


Multi-process operation mode:
1. Function calls

#!/usr/bin/env python#coding:utf-8from multiprocessing Import processimport timedef run (info_list,n): Info_ List.append (n) Print info_listinfo=[]for i in range: p = Process (target=run,args= (info,i)) P.start () print P.P Idprint p.nameprint p.is_alive ()

2. Run through class inheritance

#!/usr/bin/env python#coding:utf-8import multiprocessingimport timeclass myprocess ( Multiprocessing. Process):  #继承Process     def __init__ (self, interval):         multiprocessing. Process.__init__ (self)         self.interval = interval     def run (self):        print  "Begin----"         myfunction (Self.interval)          print  "End-----" def myfunction (ms):    n = 5     while n > 0:        print ("The time  is %s " %format (Time.ctime ()))         time.sleep (MS)         n -= 1if __init__==  ' __main__ ':     p = myprocess (1)     p.daemon =  True   #当子进程设置了daemon属性时, the default is False, the main process ends, and the child process ends     p.start ()   # When process p calls the Start () method, the Run () method is called automatically     time.sleep (3)     print  "main  Process is over .... "
Pool

Pool can provide a specified number of processes to the user, and when a new request is submitted to the pool, a new process is created to execute the request if it is not full, but if the number of processes in the pool has reached the specified maximum, the request waits until the process ends in the pool. To create a new process to it.

method:

    • Apply_async (func[, args[, kwds[, callback]]) it is non-blocking, apply (func[, args[, Kwds]) is blocked

    • Close () Close Pool so that it no longer accepts new tasks

    • Terminate () ends a worker process, does not process unfinished tasks

    • Join () The main process is blocked (suspended), waiting for the child process to exit, The Join method must be close or terminate before you can use

#!/usr/bin/env python#coding:utf-8from multiprocessing Import poolimport timedef f (x): Print x*x time.sleep (1) Re  Turn X*xpool = Pool (processes=5) #最多5个进程res_list = []print "------------" print "execution method one" #执行方法一print Pool.map (F,range (10)) #将函数放在5个进程中执行print "End-------" #执行方法二print "Execute method Two" for I in range: res = Pool.apply_async (f, (i)) #apply_async set Set to asynchronous #res. Get () #取线程执行的结果 res_list.append (res) #将执行结果放入列表中for R in Res_list: #循环列表得到结果 print r.get () print "over ..."


This article from the "Day Up goto" blog, please be sure to keep this source http://ttxsgoto.blog.51cto.com/4943095/1783926

Multi-process-multiprocess

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.