Good evening

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):

import multiprocessing

class ForeverRunner(multiprocessing.Process):
    def __init__(self, a, b, index=0):
        self.a = a
        self.b = b
        self.index = index
        super(ForeverRunner, self).__init__()

    def run(self):
        while True:
            c=self.a**self.b

for i in range(4):
    A = ForeverRunner(2,42,index=i)
    A.start()

And here’s what it looks like in htop:

Processes show their full commands that launched them. Quite hard to read.

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.

Enter setproctitle

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):

import multiprocessing
from setproctitle import setproctitle # <-- importing

class ForeverRunner(multiprocessing.Process):
    def __init__(self, a, b, index=0):
        self.a = a
        self.b = b
        self.index = index
        super(ForeverRunner, self).__init__()

    def run(self):
        setproctitle(self.__class__.__name__+str(self.index)) # <-- setting the process name
        while True:
            c=self.a**self.b

for i in range(4):
    A = ForeverRunner(2,42,index=i)
    A.start()

That’s it. Let’s see what htop shows now:

Now processes are represented by their custom names.

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.

In conclusion.

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.