Reputation: 88
I am using Python's multiprocessing library. Inside the main process, I create another process. When I do ps aux
on terminal I get two entries with same name
ps aux | grep multithreadTest
abhishek 9017 57.6 0.1 121888 11412 pts/8 Rl+ 21:09 0:04 python multithreadTest.py
abhishek 9018 16.2 0.0 48156 7096 pts/8 R+ 21:09 0:01 python multithreadTest.py
Both these processes need to communicate with each other and are doing two different tasks. It is slightly confusing which pid is doing what because they show exact same name. This is bad for troubleshooting purposes. My code is going to be deployed on production servers and will be managed by SRE guys. It might be confusing for them that why there are two instances of same task.
Is there a way I can provide a more descriptive name to be displayed on ps aux for the process that I am creating? I know it is possible in C++.
One way is to write two python scripts with different names and have a manager bash script that calls both of them. But in this way, both of them will become unrelated and managing and communicating between them will be difficult.
Any help will be appreciated.
Upvotes: 0
Views: 952
Reputation: 3435
They have the same hame since all the processes are forked from the same master process.
You need to use dedicated system tools to rename process name. E.g. setproctitle
module:
pip install setproctitle
Then in in the process function call setproctitle.setproctitle("new child process name")
.
Here is a small example:
from multiprocessing import Process
import setproctitle
import time
def a_worker():
setproctitle.setproctitle("procA")
time.sleep(10)
def b_worker():
setproctitle.setproctitle("procB")
time.sleep(10)
if __name__ == "__main__":
print("Master process title:", setproctitle.getproctitle())
a_proc = Process(target=a_worker)
b_proc = Process(target=b_worker)
# Spawn processes
a_proc.start()
b_proc.start()
# Wait for processes to terminate
a_proc.join()
b_proc.join()
print("Master process title after kids:", setproctitle.getproctitle())
Output of ps -fu
:
363 5.2 0.1 29736 10524 tty1 S 01:25 0:00 \_ python3 ab.py
370 0.0 0.0 29736 4424 tty1 S 01:25 0:00 \_ procA
371 0.2 0.0 29736 4216 tty1 S 01:25 0:00 \_ procB
Upvotes: 1