0

I have a flask project and inside that project i have notification.py which i need it to run as long as the flask project is running. The docker is connected with flask and nginx.

i tried using the following codes but they didnt work:


BASE_DIR = os.path.dirname(os.path.abspath(__file__))
notification_path = os.path.join(BASE_DIR,"notifications_script.py")

subprocess.Popen(['python', notification_path], shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

subprocess.run(['python', notification_path])

when i use subprocess.Popen it doesn't work and when i try using subprocess.run, the notifications_script start running but flask application doesnt work

if anyone can guide me into solving this problem, it will be great

Thank you

  • Why are you running Python as a subprocess of itself at all? – tripleee Oct 03 '22 at 09:50
  • i need to run a script that will work as long as flask application is working. This script will check database, make notification even if user offline and other function – mick_dev Oct 03 '22 at 09:52
  • Can you run that task in a separate container? – David Maze Oct 03 '22 at 10:06
  • issue is that most of the script function will use that same as the flask container so making new container is like repeating some of the parts again which is duplicate of teh code – mick_dev Oct 03 '22 at 10:28

1 Answers1

0

Changing Popen call to:

subprocess.Popen(['python', notification_path], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

Might do the trick.

However, note that you may want to run the new process from within a thread in order to both monitor it and let the caller keep running.

EDIT:

A per bellow comments, you can rather use the tool named supervisord in order to run your multiple programs from the docker container.

Create a configuration file which describes how to start flask and notifications_script.py and then use ENTRYPOINT of the Dockerfile to start supervisord with that configuration file.

Jib
  • 1,334
  • 1
  • 2
  • 12
  • give me a min and i will try it – mick_dev Oct 03 '22 at 09:53
  • 1
    `shell=True` is unnecessary here, and will fail miserably on non-Windows platforms. See also [Actual meaning of `shell=True` in subprocess](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Oct 03 '22 at 09:54
  • regarding the code you provided. i tested it but didnt work and i even created a new python file that only log to check it it get called but nothing in the docker logs – mick_dev Oct 03 '22 at 10:03
  • WARNING:root:The program "/app/testsubprocess.py.py" WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55e5ddd2cfb0 pid: 1 (default app) uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI master process (pid: 1) spawned uWSGI worker 1 (pid: 9, cores: 2) spawned uWSGI worker 2 (pid: 11, cores: 2) subprocess 7 exited with code 0 – mick_dev Oct 03 '22 at 10:03
  • do you know other way to run python file without supervisord because im not familair with it – mick_dev Oct 03 '22 at 10:22
  • do you have any sample of guide line i need to follow for supervisord – mick_dev Oct 03 '22 at 10:28