[英]Use four CPUs to run a python script
I'm running a python script that does some operations over a large graph, so I would like to take advantage of the 4 cores of my PC.我正在运行一个 python 脚本,该脚本在一个大图上执行一些操作,所以我想利用我 PC 的 4 个内核。 Watching the task manager I can see that all CPUs are running but the total CPU usage is up to 50%.查看任务管理器,我可以看到所有 CPU 都在运行,但总 CPU 使用率高达 50%。 As I set this PC exclusively to run this script I would like to use its CPUs as much as possible.当我将这台 PC 专门设置为运行此脚本时,我想尽可能多地使用它的 CPU。 Is there a python module or anything that can be set in my OS (windows 7) in order to allow me to do that?是否有 python 模块或可以在我的操作系统(Windows 7)中设置的任何内容,以便我这样做?
C Python has a rather generous lock that precludes most threaded operations from truly happening in parallel. C Python 有一个相当大的锁,它阻止了大多数线程操作真正并行发生。 You might want to look at the Multiprocessing module.您可能想查看Multiprocessing模块。
Otherwise, you could use a Python implementation that allows for concurrent threading:否则,您可以使用允许并发线程的 Python 实现:
Have a read through this module:阅读此模块:
http://docs.python.org/library/threading.html http://docs.python.org/library/threading.html
for very good work and a more simple example:对于非常好的工作和一个更简单的例子:
http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/ http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/
Hope these help!希望这些帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.