Multiprocessing模块原怎么在Python中使用

发布时间:2021-03-18 16:51:18 作者:Leah
来源:亿速云 阅读:225

Multiprocessing模块原怎么在Python中使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

参数:

target 是函数名字,需要调用的函数

args 函数需要的参数,以 tuple 的形式传入

用法:

multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

写一个的例子:

from multiprocessing import Pool
import os,time


def pr(str):
  print("The " + str + " is %s" %(os.getpid()))
  time.sleep(1)
  print("The " + str + " is close")


if __name__ == "__main__":

  print('-------------------------------')
  print("the current pid: "+ str(os.getpid()))
  # 默认为自己电脑的核数
  p = Pool(2)
  for i in range(5):
    p.apply_async(pr,args=('xdxd',))
  p.close()
  p.join()
  print("----------close-----------------")

通过结果可以看出,是2个进程同时启动,同时启动的进程数与pool中设置的数量和自己电脑的核数有关

结果:

-------------------------------
the current pid: 9562
The xdxd is 9563
The xdxd is 9564
The xdxd is close
The xdxd is close
The xdxd is 9563
The xdxd is 9564
The xdxd is close
The xdxd is close
The xdxd is 9563
The xdxd is close
----------close-----------------

关于Multiprocessing模块原怎么在Python中使用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. 怎么在python中实现multiprocessing多进程变量共享
  2. 如何在python中使用multiprocessing模块

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python multiprocessing

上一篇:paramiko模块如何在python中使用

下一篇:怎么在Nginx中添加lua模块

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》