Multiprocessing and process pool of multiprocessing module

The Process method of multiprocessing module

You can use the processes method to create several subprocesses in a main process

from multiprocessing import Process
import time
def f1(name):
    time.sleep(2)
    print('Hell %s' % name)
def f2(age):
    time.sleep(2)
    print('Hell %s' % age)
if __name__ == "__main__":
    p = Process(target=f1,args=('ayu',))
    p.daemon = True #If the daemon is set to True, the main process does not wait for the child process. If the main process ends, the whole process ends    
    p.start()
    p = Process(target=f2,args=('22',))
    p.daemon = True
    p.start()
    print('All Done') #Output after subprocess ends

###Memory between processes is not shared

from multiprocessing import Process
li = []
def ad(i):
    li.append(i)
    print(li)
if __name__ == "__main__":
    for i in range(10):
        p = Process(target=ad,args=(i))
        p.start()
/Users/wuxiangyu-pc/.conda/envs/test_all/bin/python /Users/wuxiangyu-pc/Documents/spider/test_all/fork_process.py
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]

It means that memory cannot be shared among processes
But the memory between threads can be shared, so the threading operation can be used

from threading import Thread
li = []
def ad(i):
    li.append(i)
    print(li)
if __name__ == "__main__":
    for i in range(10):
        p = Thread(target=ad,args=(i,))
        p.start()
/Users/wuxiangyu-pc/.conda/envs/test_all/bin/python /Users/wuxiangyu-pc/Documents/spider/test_all/fork_process.py
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Process finished with exit code 0

To achieve memory sharing between processes, you can use the Manager method

from multiprocessing import Process,Manager
def ad(i,li):
    li.append(i)
    print(li)
if __name__ == "__main__":
    manager = Manager()
    li = manager.li()
    for i in range(10):
        p = Process(target=ad,args=(i,li))
        p.start()
        p.join()
/Users/wuxiangyu-pc/.conda/envs/test_all/bin/python /Users/wuxiangyu-pc/Documents/spider/test_all/fork_process.py
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Process finished with exit code 0

##Pool process pool of multiprocessing module
Pool.apply method can implement the sequential execution of multiple subprocesses

from multiprocessing import Pool
import time
def f0(name):
    time.sleep(2)
    print('i am %s' % name)
if __name__ == "__main__":
    p = Pool(5)
    for i in range(5):
        p.apply(func=f0,args=(i,))
        print('Hello World')
    p.close()
    p.join()

Pool.apply  async implements multithreading asynchrony, with one more callback function than apply

from multiprocessing import Pool
def f1(num):
    i = num + 20
    return i
def f1(i):
    print('i am %s' % i)
if __name__ == "__main__":
    p = Pool(5)
    for i in range(5):
        p.apply_async(func=f1,args=(i,),callback=f1)
    p.close()
    p.join()

Tags: Python

Posted on Sun, 01 Dec 2019 07:24:17 -0800 by chillininvt