Git   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Python多处理只使用一个核心大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试从标准的python文档中的代码片段来学习如何使用多处理模块。 该代码粘贴在此消息的末尾。 我在四核机器上的Ubuntu 11.04上使用Python 2.7.1(根据系统监视器给我八个由于超线程引起的内核)

问题:尽pipe有几个进程已经启动,但是所有的工作量似乎都只被安排到一个核心,即接近100%的利用率。 偶尔,所有工作负载都会迁移到另一个核心,但工作负载绝不会分配到其中。

任何想法,为什么这样呢?

最好的祝福,

挂起/恢复所有用户进程 – 这可能吗?

在python中的多处理,以加快function

在C中同步进程和信号量和信号

实现与gevent兼容的工作进程池的最佳方式是什么?

多叉()并发

保罗

# # Simple example which uses a pool of workers to carry out some tasks. # # Notice that the results will probably not come out of the output # queue in the same in the same order as the corresponding tasks were # put on the input queue. If it is important to get the results BACk # in the original order then consider using `Pool.map()` or # `Pool.imap()` (which will save on the amount of code needed anyway). # # Copyright (C) 2006-2008,R Oudkerk # All rights reserved. # import time import random from multiprocessing import Process,Queue,current_process,freeze_support # # Function run by worker processes # def worker(input,output): for func,args in iter(input.get,'STOP'): result = calculate(func,args) output.put(result) # # Function used to calculate result # def calculate(func,args): result = func(*args) return '%s says that %s%s = %s' % (current_process().name,func.__name__,args,result) # # Functions referenced by tasks # def mul(a,b): time.sleep(0.5*random.random()) return a * b def plus(a,b): time.sleep(0.5*random.random()) return a + b def test(): numbER_OF_PROCESSES = 4 TASKS1 = [(mul,(i,7)) for i in range(500)] TASKS2 = [(plus,8)) for i in range(250)] # Create queues task_queue = Queue() done_queue = Queue() # Submit tasks for task in TASKS1: task_queue.put(task) # Start worker processes for i in range(numbER_OF_PROCESSES): Process(target=worker,args=(task_queue,done_queuE)).start() # Get and print results print 'Unordered results:' for i in range(len(TASKS1)): print 't',done_queue.get() # Add more tasks using `put()` for task in TASKS2: task_queue.put(task) # Get and print some more results for i in range(len(TASKS2)): print 't',done_queue.get() # Tell child processes to stop for i in range(numbER_OF_PROCESSES): task_queue.put('STOP') test()

使用os.kill将SIGINT发送到Pythonsubprocess,就像按Ctrl + C一样

在导入的多处理器子类上使用.start()时发生IOerror

有没有办法来检查一个模块是否被Windows中的多处理标准模块加载?

Python:然KeyboardInterrupt被转发到多进程subprocess?

我如何循环pipe道中的数据?

一些如何改变cpu亲和力。 之前我曾经遇到过这个问题。 我在这里找到了解决方http://bugs.python.org/issue17038#msg180663

多处理并不意味着你会使用一个处理器的所有内核,你只是得到多个进程而不是多核进程,这将由操作系统来处理,是不确定的,@Devraj发表评论有问题的答案,以完成你欲望。

我找到了一个使用并行Python的工作。 我知道这不是使用基本Python库的解决方案,但代码很简单,就像一个魅力

尝试用真正需要cpu的东西替换time.sleep ,你会看到multiprocess工作正常! 例如:

def mul(a,b): for i in xrange(100000): j = i**2 return a * b def plus(a,b): for i in xrange(100000): j = i**2 return a + b

大佬总结

以上是大佬教程为你收集整理的Python多处理只使用一个核心全部内容,希望文章能够帮你解决Python多处理只使用一个核心所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: