python并行调用多个服务_python:在多个实例上并行调用方法

这是一个如何并行调用函数的示例。从In [10]: p1.start()你可以看到输出变得混乱,因为程序p1在我们运行程序p2时并行运行。在

在Python脚本中运行程序时,要确保脚本只在所有程序都成功执行之后才结束。你可以通过

^{pr2}$

由于吉尔,Python没有C++或类似java的多线程支持。Read about it here。不过,如果你的程序执行的I/O操作比CPU密集型任务多,那么你可以使用多线程。对于执行CPU密集型任务,建议使用多处理。在

在评论中@ytutow问到工人池和过程之间有什么区别。从Pymotw:The Pool class can be used to manage a fixed number of workers for

simple cases where the work to be done can be broken up and

distributed between workers independently.

The return values from the jobs are collected and returned as a list.

The pool arguments include the number of processes and a function to

run when starting the task process (invoked once per child).

您可以将池用作:def your_instance_method(instance):

instances.do_some_computation_over_a_dataset()

with Pool(3) as p:

instances = [insatnce_1, instance_2, instance_3]

print(p.map(your_instance_method, instances))

关于正确的工作线程数,一般建议使用2*cpu内核的工作线程数。在

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值