multiprocessing创建多进程
参考 https://zhuanlan.zhihu.com/p/410731610
方法1:
#!/usr/bin/python3 # -*- 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:
#!/usr/bin/python3 # -*- coding: UTF-8 -*- from multiprocessing import Process import time import os class A(Process): def __init__(self,name, num): super().__init__() self.name=name self.num=num def run(self): for i in range(self.num): print('子进程运行中,i=%d, name=%s, pid=%d' %(i, self.name, os.getpid())) time.sleep(1) if __name__=='__main__': p1 = A('A',10) p2 = A('B',10) p1.start() p2.start() p1.join() p2.join()

浙公网安备 33010602011771号