python 多进程multiprocessing 模块

multiprocessing 经常使用方法:

  • cpu_count():统计cpu核数python

    multiprocessing.cpu_count()less

  • active_children() 获取全部子进程函数

    multiprocessing.active_children()spa

  • preces() 建立一个进程对象code

    multiprocessing.Preces(target=function_name, args=())对象

    target: 函数名  args: 函数须要的参数,以tuple形式传入,一个参数时需(1,)

Preces 经常使用方法:

  • is_alive() 判断进程是否存在进程

  • run() 启动进程ip

  • start() 启动进程,会自动调用run方法,这个经常使用get

  • join([timeout]) 等待进程结束或者直到超时
    • join() 方法说明:
    def def worker(interval): time.sleep(interval) print('hello world') P = multiprocessing.Process(target=worker, args=(5,)) #----------------------------------- P.start() #设置timeout 设置超时时间 print(P.is_alive()) P.join(timeout=3) print('end main') ### True end main hello world #----------------------------------- P.start() P.alive() # 不调置timeout 超时时间 P.join() print() ### True hello world end main #----------------------------------- 结论: 当join()不设置timeout时程序会一直等待上面的进程执行完成后再执行join()后面的代码 当设置timeout时,不管上面的进程是否执行完成,程序运行到指定时间后就会执行后面的代码 

    Preces 经常使用属性

  • namd 进程名子string

  • pid 进程的pid

相关文章
相关标签/搜索