2

I am creating a program in Python that listens to varios user interactions and logs them. I have these requirements/restrictions:

  1. I need a separate process that sends those logs to a remote database every hour
  2. I can't do it in the current process because it blocks the UI.
  3. If the main process stops, the background process should also stop.

I've been reading about subprocess but I can't seem to find anything on how to stop both simultaneously. I need the equivalent of spawn_link if anybody know some Erlang/Elixir.

Thanks!

Cristian Garcia
  • 9,630
  • 6
  • 54
  • 75
  • I know the code would be trivial, but could you show us just a minimal working example about how you spawn your subprocess? – Flavian Hautbois Feb 24 '16 at 17:26
  • Possible duplicate of [How to terminate a python subprocess launched with shell=True](http://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true) – Flavian Hautbois Feb 24 '16 at 17:31
  • OS? On windows you can use `win32process.EnumProcesses` to get a list of PID's running on the system. Your child program could accept the parent PID as a command line argument (or look up its parents' PID itself), then have an `if parentpid not in EnumProcesses(): sys.exit(0)` or something similar. – g.d.d.c Feb 24 '16 at 17:44

1 Answers1

0

To answer the question in the title (for visitors from google): there are robust solutions on Linux, Windows using OS-specific APIs and less robust but more portable psutil-based solutions.


To fix your specific problem (it is XY problem): use a daemon thread instead of a process.

A thread would allow to perform I/O without blocking GUI, code example (even if GUI you've chosen doesn't provide async. I/O API such as tkinter's createfilehandler() or gtk's io_add_watch()).

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670