我怎么知道我的 Python 进程绑定到哪个核心? [英] How do I know to which core my Python process has been bound?

查看:78
本文介绍了我怎么知道我的 Python 进程绑定到哪个核心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道我的 Python 进程绑定了哪个进程?单独这些相同的行,子进程是否将在父进程当前正在执行的同一个内核(即 CPU)上执行?

How do I know to which process my Python process has been bound? Alone these same lines, are child processes going to execute on the same core (i.e. CPU) that the parent is currently executing?

推荐答案

进程和本机操作系统线程仅在有人特别要求发生时才绑定到特定处理器.默认情况下,进程和线程可以(并且将会)在任何可用的处理器上进行调度.

Processes and native OS threads are only bound to specific processors if somebody specifically requests for that to happen. By default, processes and threads can (and will) be scheduled on any available processor.

现代操作系统使用抢占式多线程,可以随时中断线程的执行.当该线程下一次被调度运行时,它可以在不同的处理器上执行.这称为上下文切换.操作系统将线程的整个执行上下文存储起来,然后在重新调度线程时,恢复执行上下文.

Modern operating systems use pre-emptive multi-threading and can interrupt a thread's execution at any moment. When that thread is next scheduled to run, it can be executed on a different processor. This is known as a context switch. The thread's entire execution context is stored away by the operating system and then when the thread is re-scheduled, the execution context is restored.

因此,询问您的线程在哪个处理器上执行没有任何实际意义,因为答案随时可能改变.即使在查询当前线程的处理器的函数执行期间.

Because of all this, it makes no real sense to ask what processor your thread is executing on since the answer can change at any moment. Even during the execution of the function that queried which the current thread's processor.

同样,默认情况下,两个独立进程在其上执行的处理器之间没有关系.这两个进程可以在同一个处理器或不同的处理器上执行.这完全取决于操作系统如何决定调度不同的线程.

Again, by default, there's no relationship between the processors that two separate processes execute on. The two processes could execute on the same processor, or different processors. It all depends on how the OS decides to schedule the different threads.

在您声明的评论中:

由于 GIL 锁定,Python 进程将仅在一个内核上执行.

The Python process will execute on only one core due to the GIL lock.

这种说法是完全错误的.例如,一段 Python 代码会声明 GIL,在所有可用处理器之间切换上下文,然后释放 GIL.

That statement is simply incorrect. For example, a section of Python code would claim the GIL, get context switched around all the available processors, and then release the GIL.

就在答案的开头,我提到了将进程或线程绑定到特定处理器的可能性.例如,在 Windows 上,您可以使用 SetProcessAffinityMaskSetThreadAffinityMask 来执行此操作.但是,这样做是不寻常的.我只记得曾经这样做过一次,那是为了确保 CPUID 的执行在特定处理器上运行.在事物的正常运行中,进程和线程与所有处理器都有亲缘关系.

Right at the start of the answer I said alluded to the possibility of binding a process or thread to a particular processor. For example, on Windows you can use SetProcessAffinityMask and SetThreadAffinityMask to do this. However, it is unusual to do this. I can only recall ever doing this once, and that was to ensure that an execution of CPUID run on a specific processor. In the normal run of things, processes and threads have affinity with all processors.

在另一条评论中你说:

我正在创建子进程以使用 CPU 的多核.

I am creating the child processes to use multi cores of the CPU.

在这种情况下,您无需担心.通常,您会创建与逻辑处理器一样多的进程.操作系统调度程序是明智的,它将调度每个不同的进程在不同的处理器上运行.从而充分利用可用的硬件资源.

In which case you have nothing to worry about. Typically you would create as many processes as there are logical processors. The OS scheduler is sensible and will schedule each different process to run on a different processor. And thus make the optimal use of the available hardware resources.

这篇关于我怎么知道我的 Python 进程绑定到哪个核心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆