Sometimes, when I work with heavy tasks in Python using the
multiprocessing module, I want to understand what is currently being done. Since I’m on Linux (yeah, this tutorial is applicable to Linux, and most likely will not work on other operating systems. But you can give it a try on something Unix-like, like MacOS or FreeBSD), I can open a utility like
htop and it shows me the running processes. Moreover, it can show them in a convenient tree-like structure; so I can see that the main process and it s children.
Here’s the code for this example (heheh, I know, it does nothing forever, but it’s just an example):
And here’s what it looks like in
Cool. But I have no idea which subprocess is which. There is an
index argument I’ve provided and it would be nice if I could distinguish the processes with different indices.
As far as I know, there is no way to change a process name in pure Python. But, there are way of doing that in C. Since C modules are implementable into Python, the task at hand can be accomplished.
And it has been done for us already! There is a small library called setproctitle. It is installed by simply issuing
pip install setproctitle.
And the modifications we have to make are minor (shown with arrows):
That’s it. Let’s see what
htop shows now:
All the child processes are named after the class that represents them, plus the index. Superb! Now, if we need to kill a particular process, we can identify it without problems!
An important note.
It is important to pay attention to where you put that
setproctitle() call. You see, if you put it into
__init__(), it will be called in master process and change its name. This will lead to weird results. You need to call it in a method that will be called in the child process. In this very basic scenario it is
run(). That’s why I have put it in the beginning of
run(), right before the main loop.
Now you know how you can rename a process in Linux simply. It is extremely convenient for tracking the performance of child processes.
As always, thanks for reading. I will see you eventually.