How to Restart a Process in Python - Super Fast Python

You cannot restart a process in Python, instead you must create and start a new process with the same configuration.

In this tutorial you will discover how to simulate restarting a process in Python.

Let’s get started.

A process is a running instance of a computer program.

Every Python program is executed in a Process, which is a new instance of the Python interpreter. This process has the name MainProcess and has one thread used to execute the program instructions called the MainThread. Both processes and threads are created and managed by the underlying operating system.

Sometimes we may need to create new child processes in our program in order to execute code concurrently.

Python provides the ability to create and manage new processes via the multiprocessing.Process class.

You can learn more about multiprocessing in the tutorial:

There may be cases where our new processes are terminated, either normally by finishing their execution or by raising an error, and we need to restart them.

This might be for many reasons such as:

  • The process performs a useful action we wish to run periodically.
  • The process is a daemon background task that we would like to always be running.
  • The process is performing a task that can be restarted from a check-point.

Can a process be restarted in Python and if so how?

How to Restart a Process

Python process cannot be restarted or reused.

In fact, this is probably a limitation of the capabilities of processes provided by the underlying operating system.

Once a process has terminated you cannot call the start() method on it again to reuse it.

Recall that a process may terminate for many reasons such as raising an error or exception, or when it finishes executing its run() function.

Calling the start() function on a terminated process will result in an AssertionError indicating that the process can only be started once.

  • AssertionError: cannot start a process twice

Instead, to restart a process in Python, you must create a new instance of the process with the same configuration and then call the start() function.

Now that we know we cannot restart a process but must instead re-create and start a new process, let’s look at some worked examples.

Example of Restarting a Terminated Process

We can explore what happens when we attempt to start a terminated process in Python.

In this example we will create a new process to execute a target task function, wait for the new process to terminate, then attempt to restart it again. We expect it to fail with an AssertionError.

First, let’s define a target task function to execute in a new process.

The function will first block for a moment, then will report a message to let us know that the new process is executing.

# custom target function

def task():

    # block for a moment

    sleep(1)

    # report a message

    print('Hello from the new process')

Next, the main process will create an instance of the multiprocessing.Process class and configure it to execute our custom task() function via the “target” keyword.

...

# create a new process

process = Process(target=task)

We then start executing the process which will internally execute the run() function and in turn call our custom task() function.

...

# start the process

process.start()

Next, the parent process joins the new child process which will block (not return) until the new process has terminated.

...

# wait for the process to finish

process.join()

Finally, the parent process will attempt to start the terminated process again.

...

# try and start the process again

process.start()

Tying this together, the complete example is listed below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

# SuperFastPython.com

# example of restarting a process

from time import sleep

from multiprocessing import Process

# custom target function

def task():

    # block for a moment

    sleep(1)

    # report a message

    print('Hello from the new process')

# entry point

if __name__ == '__main__':

    # create a new process

    process = Process(target=task)

    # start the process

    process.start()

    # wait for the process to finish

    process.join()

    # try and start the process again

    process.start()

Running the example first creates a process instance then starts its execution.

The new process is started, blocks for a moment then reports a message.

The parent process joins the new process and waits for it to terminate. Once terminated, the parent process attempts to start the same process again.

The result is a AssertionError, as we expected.

Hello from the new process

Traceback (most recent call last):

  ...

AssertionError: cannot start a process twice

This highlights that indeed we cannot call the start() method (e.g. restart) a process that has already terminated.

Next, let’s look at an alternate approach of creating a new process instance.


Free Python Multiprocessing Course

Download your FREE multiprocessing PDF cheat sheet and get BONUS access to my free 7-day crash course on the multiprocessing API.

Discover how to use the Python multiprocessing module including how to create and start child processes and how to use a mutex locks and semaphores.

Learn more
 


Example of Restarting a Process With a New Instance

We can simulate restarting a process by creating a new process with the same configuration and starting it instead.

A new process with the same configuration can be created.

This involves arguments to the multiprocessing.Process class constructor such as:

  • Target function.
  • Target function arguments (tuple or dict).
  • Name.
  • Daemon.

If we review the source code for the multiprocessing.Process class, we can see that arguments are stored internally within the process, although are private with names like _name, _target, _args, and so on.

Instead of trying to access these private properties, we can instead just create a new process instance and specify the arguments to the constructor manually.

...

# create a new process with the same config

process2 = Process(target=task)

Then start it and join it as before.

...

# start the new process

process2.start()

# wait for the new process to finish

process2.join()

Given that we have to manually configure a new process instance, it might make sense to use a factory function that returns a new process instance with the preferred configuration in those cases where a process might need to be restarted.

For example:

# factory for creating process configured the way we like

def create_process()

return Process(target=task)

We won’t use a factory function in this case as our process has a simple configuration.

Tying this together, the complete example of simulating a process restart with a new instance is listed below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

# SuperFastPython.com

# example of restarting a process

from time import sleep

from multiprocessing import Process

# custom target function

def task():

    # block for a moment

    sleep(1)

    # report a message

    print('Hello from the new process')

# entry point

if __name__ == '__main__':

    # create a new process

    process = Process(target=task)

    # start the process

    process.start()

    # wait for the process to finish

    process.join()

    # create a new process with the same config

    process2 = Process(target=task)

    # start the new process

    process2.start()

    # wait for the new process to finish

    process2.join()

Running the example first creates the process and runs it as before.

The parent process joins the new child process until it terminates.

The parent process then creates a new child process instance with the same configuration, starts it, and joins it. As we expect, this new process executes without incident.

Hello from the new process

Hello from the new process

Further Reading

This section provides additional resources that you may find helpful.

Python Multiprocessing Books

I would also recommend specific chapters in the books:

Guides

APIs

References


    Python Multiprocessing Jump-Start

    Loving The Tutorials?

    Why not take the next step? Get the book.

    Learn more
     


    Takeaways

    You now know how to restart a process in Python.

    Do you have any questions?
    Ask your questions in the comments below and I will do my best to answer.

    Photo by Zidhan Ibrahim on Unsplash