pip install  multiprocessing  安装多进程模块

import multiprocessing,time 
import threading
def say():
    print('hhh')
def lajifenlei():
    for i in range(10):
        t=threading.Thread(target=say)
        t.start()
if __name__ =='__main__':   #多进程必须加这个
    for i in range(5):     #创建5个进程
        p=multiprocessing.Process(target=lajifenlei)
        p.start()
        print(p.pid)  #查看进程
#获取所有子进程:multiprocessing.active_children()
        print(multiprocessing.active_children())
    while len(multiprocessing.active_children()) !=0:#判断子进程数量为0,为0即子进程都运行完了。注意这里区别于线程,线程中threading.activeCount()的数量是包含主线程的,但是进程的这个方法只统计子进程
        pass
    print('子进程运行完了')

 

相关文章:

  • 2022-12-23
  • 2021-07-12
  • 2021-05-21
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-22
  • 2022-12-23
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-11-28
相关资源
相似解决方案