• Python设置进程名称


    技术背景

    当我们在同一个机器上管理多个进程时,经常会遇到一个问题是,很多进程的名称可能是重复的。以Linux系统为例,进程名称默认使用的是argv[0]。这样一来,如果在一台机器上有很多个Python任务,使用ps -a命令就可以看到大量重名的python3进程。虽然这些进程ID是独一无二的,但是光看进程ID可能无法直观的分辨是谁在执行什么样的任务。这里我们可以使用python的setproctitle库来对进程名臣进行修改,让进程名称更加的直观。

    安装与准备工作

    可以直接通过pip来安装和管理setproctitle:

    $ python3 -m pip install setproctitle
    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
    Collecting setproctitle
    Downloading https://pypi.tuna.tsinghua.edu.cn/packages/45/8d/68eec8de2d22a8ed6004344b35f94f2407ba723beee6ab468f162bb7be3e/setproctitle-1.3.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31 kB)
    Installing collected packages: setproctitle
    Successfully installed setproctitle-1.3.3

    安装完成后,我们用一个简单的示例来展示一下普通运行python代码的场景。如下代码的功能是,先打印进程ID和进程名称,然后休眠10秒时间:

    import os
    import time
    import setproctitle
    proc_title = setproctitle.getproctitle()
    print ('Process ID of this process is: {}'.format(os.getpid()))
    print ('Process title of this process is: {}'.format(proc_title))
    time.sleep(10)

    这样一来,除了可以在终端窗口上面看到程序运行的代码输出之外,我们还可以有时间在另一个终端窗口上查看ps -a来进行比对。首先看下程序输出:

    $ python3 proc_01.py
    Process ID of this process is: 1516803
    Process title of this process is: python3 proc_01.py

    然后对比一下ps -a的输出内容,首先是运行proc_01.py之前查看ps -a的内容:

    $ ps -a
    PID TTY TIME CMD
    530079 pts/2 08:37:09 nvitop
    530867 pts/0 03:05:13 top
    1516785 pts/7 00:00:00 ps

    然后是运行proc_01.py之后查看ps -a的内容:

    $ ps -a
    PID TTY TIME CMD
    530079 pts/2 08:37:09 nvitop
    530867 pts/0 03:05:13 top
    1516803 pts/4 00:00:00 python3 proc_01
    1516804 pts/7 00:00:00 ps

    这里我们发现多了一个1516803的进程,名称为python3 proc_01,跟上面程序输出的内容是一致的,只是在打印时自动省略了一些字符串的显示。

    那么到这里我们就有可能遇到背景介绍中所提到的,如果同时运行100个相同的进程任务,那么在进程名称上是无法分辨的。接下来会使用到setproctitle的修改进程名称的功能。

    修改进程名称

    我们可以使用setproctitle的setproctitle()函数,来将进程名称设置成一个独一无二的编号,类似于进程ID。而这个独一无二的ID编号,我们一般用uuid来生成:

    import os
    import uuid
    import time
    import setproctitle
    proc_title = setproctitle.getproctitle()
    print ('Process ID of this process is: {}'.format(os.getpid()))
    print ('Process title of this process is: {}'.format(proc_title))
    setproctitle.setproctitle('{}'.format(uuid.uuid1()))
    proc_title = setproctitle.getproctitle()
    print ('Process ID after modify proctitle is: {}'.format(os.getpid()))
    print ('Process title after modify is: {}'.format(proc_title))
    time.sleep(10)

    程序的运行结果如下:

    $ python3 proc_01.py
    Process ID of this process is: 1517118
    Process title of this process is: python3 proc_01.py
    Process ID after modify proctitle is: 1517118
    Process title after modify is: 73409484-a91c-11ee-9b4b-b07b25070cd2

    我们可以看到,在进程ID不变的情况下,我们成功的把进程名称修改成了一个我们想要的字符串。同样的,如果是用ps -a也能够看到被修改后的进程名称:

    $ ps -a
    PID TTY TIME CMD
    530079 pts/2 08:37:17 nvitop
    530867 pts/0 03:05:16 top
    1517118 pts/4 00:00:00 73409484-a91c-1
    1517121 pts/7 00:00:00 ps

    如果为了识别度更好一些,我们可以直接在进程名称上面把当前系统的用户名加上,这样即时是不同的用户跑同一个任务也可以进行区分。这里用户名我们通过os.getlogin()函数来获得:

    import os
    import uuid
    import time
    import setproctitle
    proc_title = setproctitle.getproctitle()
    print ('Process ID of this process is: {}'.format(os.getpid()))
    print ('Process title of this process is: {}'.format(proc_title))
    setproctitle.setproctitle('{}-{}'.format(os.getlogin(), uuid.uuid1()))
    proc_title = setproctitle.getproctitle()
    print ('Process ID after modify proctitle is: {}'.format(os.getpid()))
    print ('Process title after modify is: {}'.format(proc_title))
    time.sleep(10)

    输出结果如下:

    $ python3 proc_01.py
    Process ID of this process is: 1517417
    Process title of this process is: python3 proc_01.py
    Process ID after modify proctitle is: 1517417
    Process title after modify is: dechin-f6808444-a91c-11ee-9809-b07b25070cd2

    进程名称被成功修改。

    总结概要

    更好的管理系统进程,是每一个程序员的进阶必经之路。尤其是使用多进程、多用户的场景,系统内的进程是非常混乱的。如果在运行程序时都能控制好进程名称,那么就可以直接通过进程名称来监管和控制进程的执行和输出。本文介绍了setproctitle这样一个工具的简单使用,可以在python代码内部对进程进行管理。

    版权声明

    本文首发链接为:https://www.cnblogs.com/dechinphy/p/pname.html

    作者ID:DechinPhy

    更多原著文章:https://www.cnblogs.com/dechinphy/

    请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

    参考链接

    1. https://www.cnblogs.com/lsdb/p/13299917.html
  • 相关阅读:
    对Redis管道的理解
    python之 pyCharm pip安装pandas库失败
    Python 实现个人博客系统(附零基础python学习资料)
    Java实现负载均衡算法--轮询和加权轮询
    c++ 组合模式示例
    文章生成器免费版
    mybatis入门
    优化双重循环
    关于声子和热输运计算中BORN电荷和non-analytic修正的问题
    【matplotlib 实战】--散点图
  • 原文地址:https://www.cnblogs.com/dechinphy/p/17940059/pname