1

I have 2 python scripts I'm trying to run side by side. However, each of them have to open and close and reopen independently from each other. Also, one of the scripts is running inside a shell script.

Flaskserver.py & ./pyinit.sh

Flaskserver.py is just a flask server that needs to be restarted everynow and again to load a new page. (cant define all pages as the html is interchangeable). the pyinit is runs as xinit ./pyinit.sh (its selenium-webdriver pythoncode)

So when the Flaskserver changes and restarts the ./pyinit needs to wait about 20 seconds then restart as well.

Either one of these can create errors so I need to be able to check if Flaskserver has an error before restarting ./pyinit if ./pyinit errors i need to set the Flaskserver to a default value and then relaunch both of them.

I know a little about subprocess but I'm unsure on how it can deal with errors and stop-start code.

Amal G Jose
  • 2,486
  • 1
  • 20
  • 35
AceScottie
  • 103
  • 1
  • 12
  • Once you run these python jobs on linux shell, You can write a shell script that tracks the status of them and take actions (stop, start etc.) – Kadir Aug 20 '15 at 12:21

1 Answers1

1

Rather than using sub-process I would recommend you to create a different thread for your processes using multithread.

Multithreading will not solve the problem if global variables are colliding, but by running them in different scripts, while you might solve this, you might collide in something else like a log file.

Now, if you keep both processes running from a single process that takes care of keeping them separated and assigning different global variables where necessary, you should be able to keep a better control. Using things like join and lock from the multithreading library, will also ensure that they don't collide and it should be easy to put a process to sleep while the other is running (as per waiting 20 secs).

You can keep a thread list as a global variable, as well as your lock. I have done this successfully with CherryPy's server for example. Any more details about multithreading look into the question I linked above, it's very well explained.

Community
  • 1
  • 1
zom-pro
  • 1,571
  • 2
  • 16
  • 32
  • thanks for the answer. i dont really have a problem with variables as each code is completely separate, the server simply gets a list of htmls from a directory while the pyinit gets some variables from a rotary encoder so all global variables are read only . – AceScottie Aug 20 '15 at 13:23