我需要调试一个使用以下命令的 Python 3 程序multiprocessing
module.
我想跟踪(多核机器的)哪些核心正在使用以及如何使用。
Q:我正在寻找一种方法让 Python 代码确定哪个核心正在运行它。
我发现的最接近的是使用以下内容:
multiprocessing.current_process()._identity[0] - 1
Putting aside the fact that such code appears to be "going behind the API,"1 as far as I can tell, the code that initializes the _identity
attribute makes no reference to the underlying hardware2, which I think is unsatisfactory.
1 For one thing, I can find no official documentation for the _identity
attribute, as one would expect from the leading underscore in the name.
2 More specifically, this code evaluates something like next(_process_counter)
, where _process_counter
is initially set to the value itertools.count(1)
, and uses the the result as the basis for the _identity
attribute's value.