Python中可执行程序的核心数量

huangapple go评论104阅读模式
英文:

Number of cores for an executable in Python

问题

我正在使用这个可执行文件按顺序运行10个程序。我如何知道在运行程序时它是使用单个核心还是所有核心?

I=[i+1 for i in range(10)]
for i in I:
    try: 
        exec(open(rf"C:\Users\All_combined_Test3.py").read()) 
    except Exception as e: 
            print(f"脚本 {i} 中出现错误:{e}") 
    continue
英文:

I am using this executable to run 10 programs in sequence. How do I know if this uses a single core or all cores when running a program?

I=[i+1 for i in range(10)]
for i in I:
    try: 
        exec(open(rf"C:\Users\All_combined_Test3.py").read()) 
    except Exception as e: 
            print(f"Error in script {i}: {e}") 
    continue

答案1

得分: 1

  1. 基于你的代码,你只使用了1个核心。
  2. 在Python中没有使用多进程,只使用了1个核心。
  3. 你可以在你的代码中使用多进程,它可以使用CPU的所有核心。
  4. 如果你只需要10个核心,可以尝试使用 pool
from multiprocessing import Pool
import os
with Pool(os.cpu_count()) as p:
    pass #执行你的代码
英文:
  1. Base on your code, you just use 1 core.
  2. Only use 1 core without multiprocessing in Python.
  3. You can use multiprocessing in your code, it could use all cores of CPU.
  4. If you only need 10 cores, just try pool :

> from multiprocessing import Pool
> import os
> with Pool(os.cpu_count()) as p:
> pass #do your executable

huangapple
  • 本文由 发表于 2023年6月8日 11:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定