Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 multiprocessing terminate vs kill vs close

I get what terminate and kill do. What does close() do?

Here is the quote from the python docs:

Close the Process object, releasing all resources associated with it. ValueError is raised if the underlying process is still running. Once close() returns successfully, most other methods and attributes of the Process object will raise ValueError.

It seems to me, this is the most useless call ever:

  • if the process is still running, it does nothing, but fails with an exception

  • if the process has already terminated... It is either busy cleaning up, or finished doing that, why would I ever want to call an explicit close() on it? (Could the use of this call be OS dependent?)

Could it be that this doc entry is just extremely awkward, and somehow it does mean that close() have the power to terminate a thread, but it only does that at certain python-defined checkpoints?

like image 527
Zoltan K. Avatar asked Nov 14 '19 21:11

Zoltan K.


People also ask

What is difference between kill and terminate?

The difference between kill and terminate is that kill generally refers specifically to sending a signal, whereas terminate usually also includes other methods such as sending the process a command that tells it to exit (if the process includes a command interpreter of some kind).

How do you stop a multiprocessing process in Python?

A process can be killed by calling the Process. terminate() function. The call will only terminate the target process, not child processes. The method is called on the multiprocessing.

Which is the method used to change the default way to create child processes in multiprocessing?

The possible start methods are 'fork', 'spawn' and 'forkserver'. On Windows only 'spawn' is available. On Unix 'fork' and 'spawn' are always supported, with 'fork' being the default.

What is process in Python?

In Python, a process is an instance of the Python interpreter that executes Python code. In Python, the first process created when we run our program is called the 'MainProcess'. It is also a parent process and may be called the main parent process. The main process will create the first child process or processes.


1 Answers

The issue that added that method explains:

multiprocessing.Process (actually, the _popen object attached to it) has a GC-based finalizer to release system resources (such as fds). However, it would be nice to be able to release those resources in a timely manner. Adding a close() method would let users do that.

Basically, without close, certain resources (file descriptors used for communicating with the parent, the sentinel handle, what have you) are only closed when the Process is garbage collected, which on non-CPython interpreters, or even CPython in the case of reference cycles, might occur at some non-deterministic time in the future.

The existence of close allows you to ensure the resources are definitely cleaned at a specific point in time, so, for example, a process that is launching many Processes can ensure file descriptors are cleaned up promptly to prevent running into a ulimit or kernel imposed file descriptor limit.

The normal use case would be to call .close() immediately after .join() (or .kill() or .terminate()); the Process would eventually release the resources even if you don't do that, but it might not happen immediately.

like image 145
ShadowRanger Avatar answered Oct 17 '22 17:10

ShadowRanger