(vii) More than 7-5 processes

Source: Internet
Author: User


The difference between multi-process and multithreading
Multithreading uses a core of the CPU and is suitable for IO-intensive
Multi-process uses multiple cores of the CPU and is suitable for operation-intensive

Component
Python offers very good multi-process packages such as: multiprocessing
Multiprocessing supports the Forbidden City, communicates, shares data, performs different forms of synchronization, and provides components such as Processpipe,lock

Common methods of multiprocessing

# Multiprocessing.active_children ()   lists the existing child processes # Cpu_count ()                           Statistics CPU Number example: Import  MULTIPROCESSINGP = Multiprocessing.cpu_count () m = Multiprocessing.active_children () print  pprint m run Result: 4[]

  

Process
1. Create a Process object
p = multiprocessing. Process (Target=worker, args= (5,))
Note: target= function name
The parameters required by the args= function are passed in as a tuple
2. Process Common methods
Is_alive () Determine if the process exists
Run () Start process
Start () starts the process and automatically calls the Run method
Join (timeout) waits for the process to end or time out
3. Process Common Properties
Name process names
The PID of PID process
Example:

Import  multiprocessingimport  timedef Worker (interval):    time.sleep (interval)    print "Hell World!!!" if __name__ = = "__main__":    p = multiprocessing. Process (Target=worker, args= (5,))    P.start ()    print p.pid    print p.is_alive ()    p.join ()    # P.join ( timeout=3)     #等待紫禁城执行完毕或者超时退出    print "End main"    print p.name    print P.pid

Operation Result:

17348Truehell World!!! End mainProcess-117348

Multi-Process instances:

Import  timefrom datetime import  datetimeimport  multiprocessingnow = Datetime.strftime (DateTime.Now (), " %y-%m-%d%h:%m:%s ") def worker (Name,interval):    start = Datetime.strftime (DateTime.Now (),"%y-%m-%d%h:%m:%s ")    print "{0} start {1}". Format (Name,start)    time.sleep (interval)    end = Datetime.strftime (DateTime.Now (), " %y-%m-%d%h:%m:%s ")    print" {0} end  {1} ". Format (name,end) If __name__ = =" __main__ ":    print" main start "    print  "This  computer has {0} CPU". Format (Multiprocessing.cpu_count ())    P1 = multiprocessing. Process (target=worker,args= ("Worker1", 2))    P2 = multiprocessing. Process (target=worker,args= ("Worker2", 3))    p3 = multiprocessing. Process (target=worker,args= ("Worker3", 4))    P1.start ()    P2.start ()    p3.start ()    print  Multiprocessing.cpu_count () for    p in Multiprocessing.active_children ():        print "This pid of {0} is {1}". Format (p.name,p.pid)    Print  "main End"

Operation Result:

Main Startthis  computer have 4 cpu4this pid of Process-1 is 19872this pid of Process-3 are 19008this pid of Process-2 is 18844main End worker1 start 2017-12-08 23:40:12 worker2 start 2017-12-08 23:40:12 worker3 start 2017-12-08 23:40:12 Worke R1 End  2017-12-08 23:40:14 worker2 end  2017-12-08 23:40:15 Worker3 end  2017-12-08 23:40:16

  

(vii) More than 7-5 processes

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.