gpt4 book ai didi

python - 进程名称是从 Python 设置的,但从外部不可见

转载 作者:行者123 更新时间:2023-12-01 06:49:23 30 4
gpt4 key购买 nike

我有一个复杂的多处理 Python 项目,它启动大约 8-10 个进程。这些进程有不同的唯一名称,并且这些名称在代码中运行良好。但是,如果我检查终端中正在运行的进程,这些进程没有唯一的名称。他们的名字与其 parent 的名字相同(实际上是命令行本身)。

使用环境:

  • Python 3.6.6
  • 红帽Linux 7
  • xfce4-terminal 0.8.7.4

最少的代码:

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 脚本进程提供唯一的名称(例如:从终端)?

最佳答案

您可以使用setproctitle模块来更改进程的系统名称。显然,该模块调用了同名的 C 例程。

setproctitle.setproctitle(multiprocessing.current_process().name)

它在 Ubuntu 上适用于我。

关于python - 进程名称是从 Python 设置的,但从外部不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59091100/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com