多处理池工作程序中的线程标识符 [英] Thread identifier in multiprocessing pool workers
问题描述
我相信Thread.ident
是线程的唯一标识符,但是现在我看到multiprocessing.poo.Pool
中的不同工作进程报告了threading.current_thread().ident
相同的线程标识符.怎么样?
I believed Thread.ident
as a unique identifier of threads but now I see different worker processes in multiprocessing.poo.Pool
reporting same thread identifier by threading.current_thread().ident
. How?
推荐答案
根据平台的不同,这些ID可能是唯一的,也可能不是唯一的.这里要注意的重要一点是python多重处理库实际上使用进程而不是线程进行多重处理,因此进程之间的线程id实际上是特定于平台的实现细节.
Depending on the platform, the ids may or may not be unique. The important thing to note here is that the python multiprocessing library actually uses processes instead of threads for multiprocessing, and so thread ids in between processes is actually a platform-specific implementation detail.
在Unix/Linux上:保证线程ID在单个进程内唯一.但是,不能保证线程ID在进程之间是唯一的.但是,processid(pid)在各个进程之间将是唯一的.因此,您可以通过将两者放在一起获得唯一的标识符. man pthread
页面上的详细信息 http://man7.org/linux/man-pages/man7/pthreads.7.html
On Unix/Linux: a thread id is guaranteed to be unique inside a single process. However, a thread id is not guaranteed to be unique across processes. The processid (pid), however, will be unique across processes. Thus, you can obtain a unique identifier by putting the two together. Detail from the man pthread
page http://man7.org/linux/man-pages/man7/pthreads.7.html
On windows: a thread id is unique across the whole machine: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686746(v=vs.85).aspx
这篇关于多处理池工作程序中的线程标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!