为什么我的Python程序平均每个进程仅占用33%的CPU?如何使Python使用所有可用的CPU? [英] Why does my Python program average only 33% CPU per process? How can I make Python use all available CPU?

查看:781
本文介绍了为什么我的Python程序平均每个进程仅占用33%的CPU?如何使Python使用所有可用的CPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 2.5.4.我的计算机:CPU AMD Phenom X3 720BE,主板780G,4GB RAM,Windows 7 32位.

I use Python 2.5.4. My computer: CPU AMD Phenom X3 720BE, Mainboard 780G, 4GB RAM, Windows 7 32 bit.

我使用Python线程,但不能使每个python.exe进程消耗100%的CPU.为什么他们平均只使用大约33-34%?

I use Python threading but can not make every python.exe process consume 100% CPU. Why are they using only about 33-34% on average?.

我希望将所有可用的计算机资源用于这些大型计算,以便尽快完成它们.

I wish to direct all available computer resources toward these large calculations so as to complete them as quickly as possible.

谢谢大家.现在,我正在使用Parallel Python,并且一切正常.我的CPU现在总是100%.谢谢大家!

Thanks everybody. Now I'm using Parallel Python and everything works well. My CPU now always at 100%. Thanks all!

推荐答案

看来您有一个3核CPU.如果要在本机Python代码中使用多个CPU内核,则必须产生多个进程. (两个或多个Python线程不能在不同的CPU上同时运行)

It appears that you have a 3-core CPU. If you want to use more than one CPU core in native Python code, you have to spawn multiple processes. (Two or more Python threads cannot run concurrently on different CPUs)

R. Pate 说,Python的multiprocessing模块是一种方法.但是,我建议改用 并行Python .它负责分配任务和传递消息.您甚至可以在许多不同的计算机上运行任务,而无需更改代码.

As R. Pate said, Python's multiprocessing module is one way. However, I would suggest looking at Parallel Python instead. It takes care of distributing tasks and message-passing. You can even run tasks on many separate computers with little change to your code.

使用它非常简单:

import pp

def parallel_function(arg):
    return arg

job_server = pp.Server() 

# Define your jobs
job1 = job_server.submit(parallel_function, ("foo",))
job2 = job_server.submit(parallel_function, ("bar",))

# Compute and retrieve answers for the jobs.
print job1()
print job2()

这篇关于为什么我的Python程序平均每个进程仅占用33%的CPU?如何使Python使用所有可用的CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆