简体   繁体   English

Python:我的具有五个进程的 python 程序使用了多少个内核?

[英]Python: How many cores are used by my python program with five processes?

I have a python program consisting of 5 processes outside of the main process.我有一个 python 程序,由主进程之外的 5 个进程组成。 Now I'm looking to get an AWS server or something similar on which I can run the script.现在我正在寻找一个 AWS 服务器或类似的东西,我可以在其上运行脚本。 But how can I find out how many vCPU cores are used by the script/how many are needed?但是我怎样才能知道脚本使用了多少个 vCPU 内核/需要多少个? I have looked at:我看过:

import multiprocessing

multiprocessing.cpu_count()

But it seems that it just returns the CPU count that's on the system.但它似乎只返回系统上的 CPU 计数。 I just need to know how many vCPU cores the script uses.我只需要知道脚本使用了多少个 vCPU 内核。

Thanks for your time.谢谢你的时间。

EDIT:编辑:

Just for some more information.只是为了获得更多信息。 The Processes are running indefinitely.进程无限期地运行。

Your question uses some general terms and leaves much unspecified so answers must be general.您的问题使用了一些通用术语并且未指定很多内容,因此答案必须是通用的。

It is assumed you are managing the processes using either Process directly or ProcessPoolExecutor.假设您正在使用 Process 直接或 ProcessPoolExecutor 管理进程。

In some cases, vCPU is a logical processor but per the following link there are services offering configurations of fractional vCPUs such as those in shared environments...在某些情况下,vCPU 是一个逻辑处理器,但根据以下链接,有一些服务提供部分 vCPU 的配置,例如共享环境中的那些...

What is vCPU in AWS AWS 中的 vCPU 是什么

You mention/ask...你提到/问...

... Now I'm looking to get an AWS server or something similar on which I can run the script. ...现在我正在寻找一个 AWS 服务器或类似的东西,我可以在其上运行脚本。 ... ...

... But how can I find out how many vCPU cores are used by the script/how many are needed? ...但是我怎样才能找出脚本使用了多少个 vCPU 内核/需要多少个? ... ...

You state AWS or something like it.你 state AWS 或类似的东西。 The answer would depend on what your subprocess do, and how much of a vCPU or factional vCPU each subprocess needs.答案取决于您的子进程做什么,以及每个子进程需要多少 vCPU 或派系 vCPU。 Generally, a vCPU is analogous to a logical processor upon which a thread can execute.通常,vCPU 类似于可以在其上执行线程的逻辑处理器。 A fractional portion of a vCPU will be some limited usage (than some otherwise "full" or complete "usage") of a vCPU. vCPU 的一小部分将是 vCPU 的一些有限使用(而不是一些其他“完全”或完全“使用”)。

The meaning of one or more vCPUs (or fractional vCPUs thereto) to your subprocesses really depends on those subprocesses, what they do.一个或多个 vCPU(或其中的部分 vCPU)对您的子进程的含义实际上取决于这些子进程,它们做什么。 If one subprocess is sitting waiting on I/O most of the time, you hardly need a dedicated vCPU for it.如果一个子进程大部分时间都在等待 I/O,那么您几乎不需要专门的 vCPU 来处理它。

I recommend starting with some minimal least expensive configuration and see how it works with your app's expected workload.我建议从一些最低成本的配置开始,看看它如何与您的应用程序的预期工作负载一起工作。 If you are not happy, increase the configuration as needed.如果您不满意,请根据需要增加配置。

If it helps...如果有帮助...

I usually use subprocesses if I need simultaneous execution that avoids Python's GIL limitations by breaking things into subprocesses.如果我需要通过将事物分解为子进程来避免 Python 的 GIL 限制的同时执行,我通常会使用子进程。 I generally use a single active thread per subprocess, where any other threads in the same subprocess are usually at a wait, waiting for I/O or do not otherwise compete with the primary active thread of the subprocess.我通常为每个子进程使用一个活动线程,其中同一子进程中的任何其他线程通常处于等待状态,等待 I/O 或不与子进程的主要活动线程竞争。 Of course, a subprocess could be dedicated to I/O if you want to separate such from other threads you place in other subprocesses.当然,如果您想将 I/O 与放置在其他子进程中的其他线程分开,则子进程可以专用于 I/O。

Since we do not know your app's purpose, architecture and many other factors, it's hard to say more than the generalities above.由于我们不知道您的应用程序的用途、架构和许多其他因素,因此很难说比上面的概括性更多。

Answer to this post probably lies in the following question:这篇文章的答案可能在于以下问题:

Multiprocessing: More processes than cpu.count多处理:比 cpu.count 更多的进程

In short, you have probably hundreds of processes running, but that doesn't mean you will use hundreds of cores.简而言之,您可能有数百个进程正在运行,但这并不意味着您将使用数百个内核。 It all depends on utilization, and the workload of the processes.这完全取决于利用率和流程的工作量。

You can also get some additional info from the psutil module您还可以从 psutil 模块获取一些附加信息

import psutil

print(psutil.cpu_percent())
print(psutil.cpu_stats())
print(psutil.cpu_freq())

or using OS to receive current cpu usage in python:或使用操作系统接收 python 中的当前 CPU 使用情况:

import os
import psutil

l1, l2, l3 = psutil.getloadavg()
CPU_use = (l3/os.cpu_count()) * 100

print(CPU_use)
  • Credit: DelftStack学分:代尔夫特堆栈

Edit编辑

There might be some information for you in the following medium article.以下媒体文章可能会为您提供一些信息。 Maybe there are some tools for CPU usage too.也许也有一些用于 CPU 使用的工具。 https://medium.com/survata-engineering-blog/monitoring-memory-usage-of-a-running-python-program-49f027e3d1ba https://medium.com/survata-engineering-blog/monitoring-memory-usage-of-a-running-python-program-49f027e3d1ba

On Linux you can use the "top" command at the command line to monitor the real-time activity of all threads of a process id:在 Linux 上,您可以在命令行中使用“top”命令来监控进程 id 的所有线程的实时活动:

top -H -p <process id>

I'll try to do my own summary about "I just need to know how many vCPU cores the script uses" .我将尝试对“我只需要知道脚本使用多少个 vCPU 内核”进行自己的总结。

There is no way to answer that properly other than running your app and monitoring its resource usage.除了运行您的应用程序并监控其资源使用情况之外,没有办法正确回答这个问题。 Assuming your Python processes do not spawn subprocesses (which could even be multithreaded applications), all we can say is that your app won't utilize more than 6 cores (as per total number of processes).假设您的 Python 进程不会产生子进程(甚至可能是多线程应用程序),我们只能说您的应用程序不会使用超过 6 个内核(根据进程总数)。 There's a ton of ways for program to under-utilize CPU cores, like waiting for I/O (disk or network) or interprocess synchronization (shared resources).程序有很多方法可以充分利用 CPU 内核,例如等待 I/O(磁盘或网络)或进程间同步(共享资源)。 So to get any kind of understanding of CPU utilization, you really need to measure the actual performance (eg, with htop utility on Linux or macOS) and investigating the causes of underperforming (if any).因此,要了解 CPU 利用率,您确实需要测量实际性能(例如,使用 Linux 或 macOS 上的htop实用程序)并调查性能不佳的原因(如果有的话)。

Hope it helps.希望能帮助到你。

Your computer has hundreds if not thousands of processes running at any given point.您的计算机在任何给定点都有数百个(如果不是数千个)进程在运行。 How does it handle all of those if it only has 5 cores?如果它只有 5 个内核,它如何处理所有这些问题? The thing is, each core takes a process for a certain amount of time or until it has nothing left to do inside that process.问题是,每个核心都需要一个进程一段时间,或者直到它在该进程中无事可做。

For example, if I create a script that calculates the square root of all numbers from 1 to say a billion, you will see that a single core will hit max usage, then a split second later another core hits max while the first drops to normal and so on until the calculation is done.例如,如果我创建一个脚本来计算从 1 到十亿的所有数字的平方根,你会看到一个核心将达到最大使用率,然后一瞬间另一个核心达到最大使用率,而第一个核心下降到正常以此类推,直到计算完成。

Or if the process waits for an I/O process, then the core has nothing to do, so it drops the process, and goes to another process, when the I/O operation is done, the core can pick the process back, and get back to work.或者如果进程在等待一个 I/O 进程,那么核心无事可做,所以它会丢弃该进程,然后转到另一个进程,当 I/O 操作完成时,核心可以重新选择该进程,并且回去工作。

You can run your multiprocessing python code on a single core, or on 100 cores, you can't really do much about it.您可以在单个内核或 100 个内核上运行您的多处理 python 代码,您真的无能为力。 However, on windows, you can set affinity of a process, which gives the process access to certain cores only.但是,在 windows 上,您可以set affinity ,这使进程只能访问某些内核。 So, when the processes start, you can go to each one and set the affinity to say core 1 or each one to a separate core.因此,当进程开始时,您可以将 go 设置为每个,并将关联设置为核心 1 或每个单独的核心。 Not sure how you do that on Linux though.不过,不确定你是如何在 Linux 上做到这一点的。

In conclusion, if you want a short and direct answer, I think we can say as many cores as it has access to.总之,如果您想要一个简短而直接的答案,我认为我们可以说它可以访问尽可能多的内核。

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

相关问题 我的Python进程在哪些CPU内核上运行? - On what CPU cores are my Python processes running? 当我在 python 中有 8 个逻辑核心时,将我的程序分成 8 个独立的进程是最好的方法(性能方面)吗? - Is spliting my program into 8 separate processes the best approach (performance wise) when I have 8 logical cores in python? 如何在多处理中将CPU内核分配给python进程? - How do CPU cores get allocated to python processes in multiprocessing? Python:如果光标处于非活动状态五分钟,如何控制光标并在用户触摸鼠标时暂停程序(我的 Python 程序)? - Python: How to take control of the cursor if cursor is inactive for five minutes and pause the program (my python program) if user touches the mouse? 在python中的不同内核上分布多个进程 - Distribute multiple processes over different cores in python Python多处理比核心启动更多的进程 - Python multiprocessing start more processes than cores 为Python程序分配多个内核 - Assigning Multiple Cores to a Python Program 在多个内核上运行python程序 - Run a python program on multiple cores 如何限制 python 库中使用的 CPU 内核数量? - How to limit the amount of CPU cores used in a python library? 如何杀死 Python 程序的所有进程? - How to kill all processes of a Python program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM