0

I am using subprocess.Popen in a Django application. I then store the pid in a database. The user can then send a rest request to /api/task/task.id/stop/ in order to kill the task.

This all works fine, except that when I kill the subprocess the Django development server is also terminated. How do I go about killing the subprocess without killing the dev server?

I start my process like this:

process = subprocess.Popen(["nohup {} {} {}".format(settings.PYTHON, "task.py", task.id) ], shell=True )

task.pid = process.pid

task.save()

And I am terminating it like this:

os.killpg( os.getpgid(task.pid), signal.SIGTERM )
  • 1
    use celery for async tasks – vorujack Sep 29 '19 at 03:39
  • Is it not possible to do it like I am attempting? I have looked into celery but my aim is to distribute this bundled as part of an Electron application - so I would like to avoid celery. –  Sep 29 '19 at 07:55
  • https://stackoverflow.com/a/4791612 <- Answered here. I was missing the `preexec_fn=os.setsid` –  Sep 29 '19 at 08:03

0 Answers0