简体   繁体   English

为什么我的 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?

I use Python 2.5.4.我使用 Python 2.5.4。 My computer: CPU AMD Phenom X3 720BE, Mainboard 780G, 4GB RAM, Windows 7 32 bit.我的电脑:CPU AMD Phenom X3 720BE,主板 780G,4GB RAM,Windows 7 32 位。

I use Python threading but can not make every python.exe process consume 100% CPU.我使用 Python 线程,但不能让每个 python.exe 进程消耗 100% CPU。 Why are they using only about 33-34% on average?.为什么他们平均只使用大约 33-34%?

I wish to direct all available computer resources toward these large calculations so as to complete them as quickly as possible.我希望将所有可用的计算机资源用于这些大型计算,以便尽快完成它们。

EDIT: Thanks everybody.编辑:谢谢大家。 Now I'm using Parallel Python and everything works well.现在我正在使用 Parallel Python 并且一切正常。 My CPU now always at 100%.我的 CPU 现在总是 100%。 Thanks all!谢谢大家!

It appears that you have a 3-core CPU. 看来你有一个3核CPU。 If you want to use more than one CPU core in native Python code, you have to spawn multiple processes. 如果要在本机Python代码中使用多个CPU内核,则必须生成多个进程。 (Two or more Python threads cannot run concurrently on different CPUs) (两个或多个Python线程无法在不同的CPU上并发运行)

As R. Pate said, Python's multiprocessing module is one way. 正如R. Pate所说,Python的multiprocessing模块是一种方式。 However, I would suggest looking at Parallel Python instead. 但是,我建议改为使用Parallel Python It takes care of distributing tasks and message-passing. 它负责分发任务和消息传递。 You can even run tasks on many separate computers with little change to your code. 您甚至可以在许多单独的计算机上运行任务,而对代码的更改很少。

Using it is quite simple: 使用它很简单:

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()

Try the multiprocessing module, as Python, while it has real, native threads, will restrict their concurrent use while the GIL is held. 尝试多处理模块,就像Python一样,虽然它有真正的本机线程,但在GIL被保持时会限制它们的并发使用。 Another alternative, and something you should look at if you need real speed, is writing a C extension module and calling functions in it from Python. 另一种选择,如果你需要真正的速度,你应该看一下,就是编写C扩展模块并从Python中调用函数。 You can release the GIL in those C functions. 您可以在这些C函数中释放GIL。

Also see David Beazley 's Mindblowing GIL . 另见David BeazleyMindblowing GIL

Global Interpreter Lock 全球口译员锁

The reasons of employing such a lock include: 使用这种锁的原因包括:

 * increased speed of single-threaded programs (no necessity to acquire or release locks on all data structures separately) * easy integration of C libraries that usually are not thread-safe. 

Applications written in languages with a GIL have to use separate processes (ie interpreters) to achieve full concurrency, as each interpreter has its own GIL. 用GIL语言编写的应用程序必须使用单独的进程(即解释器)来实现完全并发,因为每个解释器都有自己的GIL。

From CPU usage it looks like you're still running on a single core. 从CPU使用情况看,您仍然在单核上运行。 Try running a trivial calculation with 3 or more threads with same threading code and see if it utilizes all cores. 尝试使用具有相同线程代码的3个或更多线程运行一个简单的计算,看看它是否使用了所有内核。 If it doesn't, something might be wrong with your threading code. 如果没有,您的线程代码可能出错。

Stackless Python怎么样?

您可能在其他地方遇到瓶颈问题,例如硬盘驱动器(分页)或内存访问。

You should perform some Operating System and Python monitoring to determine where the bottle neck is. 您应该执行一些操作系统和Python监视来确定瓶颈的位置。

Here is some info for windows 7: 以下是Windows 7的一些信息:

Performance Monitor : You can use Windows Performance Monitor to examine how programs you run affect your computer's performance, both in real time and by collecting log data for later analysis. 性能监视器 :您可以使用Windows性能监视器来检查运行的程序如何实时影响计算机的性能,并通过收集日志数据以供以后分析。 ( Control Panel-> All Control Panel Items->Performance Information and Tools-> Advanced Tools- > View Performance Monitor) 控制面板 - >所有控制面板项目 - >性能信息和工具 - >高级工具 - >查看性能监视器)

Resource Monitor : Windows Resource Monitor is a system tool that allows you to view information about the use of hardware (CPU, memory, disk, and network) and software (file handles and modules) resources in real time. 资源监视器 :Windows资源监视器是一个系统工具,允许您实时查看有关硬件(CPU,内存,磁盘和网络)和软件(文件句柄和模块)资源使用的信息。 You can use Resource Monitor to start, stop, suspend, and resume processes and services. 您可以使用资源监视器来启动,停止,暂停和恢复进程和服务。 ( Control Panel-> All Control Panel Items->Performance Information and Tools-> Advanced Tools- > View Resource Monitor ) 控制面板 - >所有控制面板项目 - >性能信息和工具 - >高级工具 - >查看资源监视器

I solved the problems that led me to this post by running a second script manually.我通过手动运行第二个脚本解决了导致我写这篇文章的问题。 This post helped me run multiple python scripts at the same time .这篇文章帮助我同时运行多个 python 脚本

I managed to execute in the newly-opened terminal window typing a command there.我设法在新打开的终端窗口中执行,在那里输入命令。 Not as convenient as shift + enter but does the job.不如 shift + enter 方便,但可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM