Thread identifier in multiprocessing pool workers

时光怂恿深爱的人放手 提交于 2019-12-12 05:06:47

问题


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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/38949614/thread-identifier-in-multiprocessing-pool-workers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!