简体   繁体   English

带有线程的4核PC的高效Python编程?

[英]Efficient Python programming for 4-core PC with threading?

I'm looking to write a script to explore the parameter space in some simulation software. 我正在寻找编写脚本来探索某些仿真软件中的参数空间。 Initially, I just want to run the simulation script, say, 100 times, each time incrementing one parameter. 最初,我只想运行模拟脚本,例如100次,每次递增一个参数。

This is to run on a machine not being used for anything else, so ideally I'd make use of all four of its cores to speed up processing. 这是要在没有其他用途的机器上运行,因此理想情况下,我将利用其所有四个内核来加快处理速度。

Could anyone recommend an efficient way of doing this please? 有人可以推荐一种有效的方法吗?

I considered generating 100 python scripts and then queuing them in a bash script. 我考虑过生成100个python脚本,然后在bash脚本中对其进行排队。 Alternatively, I could write a single python script to perform everything in a FOR loop. 另外,我可以编写一个Python脚本来执行FOR循环中的所有操作。 I thought I could use threading to allow 4 operations to be performed at once. 我以为我可以使用线程允许一次执行4个操作。 IF this seems sensible, is the best implementation method to hard-code into the software to make and handle 4 threads, or is there a clever way for Python to handle CPU usage efficiently? 如果这看起来很明智,是将代码硬编码到软件中以制造和处理4个线程的最佳实现方法,还是Python能够有效处理CPU使用率的聪明方法?

Many thanks, 非常感谢,

You should use multiprocessing module instead of threading . 您应该使用multiprocessing模块而不是threading

http://docs.python.org/2/library/multiprocessing.html http://docs.python.org/2/library/multiprocessing.html

You can create multiple processes, it is up to the system to optimize CPU usage. 您可以创建多个进程,这取决于系统来优化CPU使用率。

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

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