multiprocessing创建多进程

发布时间 2023-11-17 20:58:10作者: ho966

参考 https://zhuanlan.zhihu.com/p/410731610

方法1:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from multiprocessing import Process 
import time
import os
def test_proc(num):
    for i in range(num):
        print('子进程运行中,i=%d, name=%s, pid=%d' %(i, __name__, os.getpid()))
        time.sleep(1)

if __name__=='__main__':
    p1 = Process(target = test_proc, name = 'test1', args=( 10, ))
    p2 = Process(target = test_proc, name = 'test2', args=( 10, ))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    

方法2: