我有一个复杂的多处理Python项目,它大约启动8-10个进程。这些进程得到不同的唯一名称,这些名称在代码中工作得很好。但是,如果我检查终端中正在运行的进程,这些进程就没有唯一的名称。它们的名称与父名称相同(实际上是命令行本身)。
使用环境:
--最小代码:
import multiprocessing
import time
def sleeping_function(s_time):
multiprocessing.current_process().name = "process_{}".format(s_time)
print(multiprocessing.current_process().name)
time.sleep(s_time)
if __name__ == "__main__":
times = [200, 201, 202, 203, 204]
with multiprocessing.Pool(processes=len(times)) as pool:
results = pool.map(sleeping_function, times)输出:
>>> python3 test.py
process_200
process_201
process_202
process_203
process_204检查我的终端中正在运行的进程(当所有进程在脚本中运行时)。
命令:
ps aux -u my_user_name | grep python输出:
my_user_name 888273 0.1 0.0 359804 11288 pts/162 Sl+ 15:10 0:00 python3 test.py
my_user_name 888274 0.0 0.0 138608 8040 pts/162 S+ 15:10 0:00 python3 test.py
my_user_name 888275 0.0 0.0 138608 8056 pts/162 S+ 15:10 0:00 python3 test.py
my_user_name 888276 0.0 0.0 138608 8056 pts/162 S+ 15:10 0:00 python3 test.py
my_user_name 888277 0.0 0.0 138608 8060 pts/162 S+ 15:10 0:00 python3 test.py
my_user_name 888278 0.0 0.0 138608 8060 pts/162 S+ 15:10 0:00 python3 test.py我希望看到process_20X进程名,而不是调用的Python命令。
我的问题:
是否有可能从Python脚本为进程提供唯一的名称,这些进程在外部是可见的(例如:从终端)?
发布于 2019-11-28 15:16:56
您可以使用集程序标题模块更改进程的系统名称。显然,模块以相同的名称调用C例程。
setproctitle.setproctitle(multiprocessing.current_process().name)它在Ubuntu上对我有用。
https://stackoverflow.com/questions/59091100
复制相似问题