Python multiprocessing.Process

multiprocessing.Process

multiprocessing是python的中的一个多进程管理库,multiprocessing.Process模块用于创建进程

使用方法:

Process(target=, args=)
  • target 要执行的函数
  • 传入函数的参数,必须是 iterable

支持的函数:

  • is_alive() 判断进程是否存活
  • run() 创建进程未指定 target ,默认执行run()
  • start() 执行进程
  • join() 阻塞当前进程,直到等待的进程执行完毕再执行当前
  • terminate() 停止进程

示例:

import os
from multiprocessing import Process

def fun(n):
    print("number is %d, process id is %d" % (n, os.getpid()))
if __name__ == '__main__':
    print(os.getpid())

    five_process = []
    for i in range(5):
        p = Process(target=fun, args=(i, ))
        p.start()
        five_process.append(p)
    for p in five_process:
        p.join()
    print("=====end=====")

输出:

2332
number is 0, process id is 9768
number is 2, process id is 2924
number is 1, process id is 1292
number is 4, process id is 1960
number is 3, process id is 204
=end=

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值