Multi-process and process pools

Source: Internet
Author: User

From multiprocessing import Process

Import OS


# code to be executed by the child process

def run_proc (name):

print ' Run child process%s (%s) ... '% (name, Os.getpid ())


If __name__== ' __main__ ':

print ' Parent process%s. '% Os.getpid ()

p = Process (Target=run_proc, args= (' Test ',))

print ' Process'll start. '

P.start ()

P.join ()

print ' Process end. '



When you create a child process, you only need to pass in an argument that executes the function and function, create an instance, start with the start () method, and make the process simpler than fork ().

The join () method can wait for the child process to end before continuing to run, typically for inter-process synchronization.



Process Pool

Pool


If you want to start a large number of child processes, you can create the child processes in batches using the process pool:


From multiprocessing import Pool

Import OS, time, random


def long_time_task (name):

print ' Run task%s (%s) ... '% (name, Os.getpid ()) request_uri/provider/reportlocation

Start = Time.time ()

Time.sleep (Random.random () * 3)

End = Time.time ()

print ' Task%s runs%0.2f seconds. '% (name, (End-start))


If __name__== ' __main__ ':

print ' Parent process%s. '% Os.getpid ()

p = Pool ()

For I in range (5):

P.apply_async (Long_time_task, args= (i,))

print ' Waiting for all subprocesses done ... '

P.close ()

P.join ()

print ' All subprocesses done. '


Code interpretation:


Calling the Join () method on the pool object waits for all child processes to complete, must call Close () before calling join (), and cannot continue adding a new process after calling Close ().


Note that the result of the output, task 0,1,2,3 is executed immediately, and task 4 waits for the previous task to complete before it executes, because the default size of pool is 4 on my computer, so up to 4 processes are executed at the same time. This is a design restriction by the pool and is not a limitation of the operating system. If you change to:

p = Pool (5)

You can run 5 processes at a time.


This article is from the DBA Sky blog, so be sure to keep this source http://9425473.blog.51cto.com/9415473/1698866

Multi-process and process pools

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.