Hướng dẫn how to restart python
Show Last Updated on September 12, 2022 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:
Can a process be restarted in Python and if so how? Got slow loops? Run your loops in parallel (using all
CPUs) How to Restart a ProcessPython 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.
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. Confused by the multiprocessing module API? Example of Restarting a Terminated ProcessWe 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.
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.
We then start executing the process which will internally execute the run() function and in turn call our custom task() function.
Next, the parent process joins the new child process which will block (not return) until the new process has terminated.
Finally, the parent process will attempt to start the terminated process again.
Tying this together, the complete example is listed below.
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.
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. Need help with Python Multiprocessing? Sign-up to my FREE 7-day email course. Discover how to use the Python multiprocessing module, including how to create and start child processes, how to use a mutex and semaphore, and much more! Click the button below and enter your email address to sign-up and get the first lesson right now. Start Your FREE Email Course Now! Example of Restarting a Process With a New InstanceWe 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:
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.
Then start it and join it as before.
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:
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.
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.
Further ReadingThis section provides additional resources that you may find helpful.
TakeawaysYou now know how to restart a process in Python. Do
you have any questions? Photo by Zidhan Ibrahim on Unsplash |