Python multi-process execution

from multiprocessing import Process
import time

def f_square(n):
    time.sleep(1)
    print n*n

if __name__=='__main__':
    for i in range(10):
        p = Process(target=f_square,args=[i,])
        p.start()
        print "p.pid:", p.pid
        print "p.name:", p.name
        print "p.is_alive:", p.is_alive()
p.pid: 109672
p.name: Process-1
p.is_alive: True
p.pid: 108304
p.name: Process-2
p.is_alive: True
p.pid: 110720
p.name: Process-3
p.is_alive: True
p.pid: 107916
p.name: Process-4
p.is_alive: True
p.pid: 106136
p.name: Process-5
p.is_alive: True
p.pid: 104820
p.name: Process-6
p.is_alive: True
p.pid: 113520
p.name: Process-7
p.is_alive: True
p.pid: 112672
p.name: Process-8
p.is_alive: True
p.pid: 113440
p.name: Process-9
p.is_alive: True
p.pid: 111076
p.name: Process-10
p.is_alive: True

If you want to display the results of the process execution function, you can only print the results of 0, 1, 4, 9.... by running the .py file through the Python console. This is a problem with multiple processes in Python under Windows.

For more information on multi-process operations, refer to this blog post: https://www.cnblogs.com/kaituorensheng/p/4445418.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763730&siteId=291194637