0

I am using a Python process to run one of my functions like so:

Process1 = Process(target = someFunction)
Process1.start()

Now that function has no looping or anything, it just does its thing, then ends, does the Process die with it? or do I always need to drop a:

Process1.terminate()

Afterwards?

Aphire
  • 1,621
  • 25
  • 55
  • All the information you need is in the documentation https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process.terminate – Padraic Cunningham Jul 10 '15 at 15:10

2 Answers2

2

The child process will exit by itself - the Process1.terminate() is unnecessary in that regard. This is especially true if using any shared resources between the child and parent process. From the Python documentation:

Avoid terminating processes

Using the Process.terminate method to stop a process is liable to cause any shared resources (such as locks, semaphores, pipes and queues) currently being used by the process to become broken or unavailable to other processes.

Therefore it is probably best to only consider using Process.terminate on processes which never use any shared resources.

However, if you want the parent process to wait for the child process to finish (perhaps the child process is modifying something that the parent will access afterwards), then you'll want to use Process1.join() to block the parent process from continuing until the child process complete. This is generally good practice when using child processes to avoid zombie processes or orphaned children.

Community
  • 1
  • 1
tonysdg
  • 1,335
  • 11
  • 32
1

No, as per the documentation it only sends a SIGTERM or TerminateProcess() to the process in question. If it has already exited then there is nothing to terminate.

However, it is always a good process to use exit codes in your subprocesses:

import sys
sys.exit(1)

And then check the exit code once you know the process has terminated:

if Process1.exitcode():
   errorHandle()
amza
  • 780
  • 2
  • 7
  • 32