I have a JavaScript application running on a Python / PyQt / QtWebKit foundation which creates subprocess.Popen
objects to run external processes.
Popen
objects are kept in a dictionary and referenced by an internal identifier so that the JS app can call Popen
's methods via a pyqtSlot
such as poll()
to determine whether the process is still running or kill()
to kill a rogue process.
If a process is not running any more, I would like to remove its Popen
object from the dictionary for garbage collection.
What would be the recommended approach to cleaning up the dictionary automatically to prevent a memory leak ?
My ideas so far:
- Call
Popen.wait()
in a thread per spawned process to perform an automatic cleanup right upon termination.
PRO: Immediate cleanup, threads probably do not cost much CPU power as they should be sleeping, right ?
CON: Many threads depending on spawning activity. - Use a thread to call
Popen.poll()
on all existing processes and checkreturncode
if they have terminated and clean up in that case.
PRO: Just one worker thread for all processes, lower memory usage.
CON: Periodic polling necessary, higher CPU usage if there are many long-running processes or lots of processed spawned.
Which one would you choose and why ? Or any better solutions ?