Issues with python scripts running simultaneously





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have two python scripts that use two different cameras for a project I am working on and I am trying to run them both inside a different script or within each other, either way is fine.



import os

os.system('python 1.py')

os.system('python 2.py')


My problem however is that they don't run at the same time, I have to quit the first one for the next to open. I also tried doing it with bash as well with the & shell operator



python 1.py &
python 2.py &


And this does in fact make them both run however the issue is that they both run endlessly in the background and I need to close them rather easily. Any suggestion what I can do to avoid the issues with these implementations










share|improve this question























  • Because your python scripts are programs that never ends until user doesn't decide to close them or error raises your first statement blocks your calling program. So until you don't close python1.py obviously python will never go to execute the next statement that in your case is another endless program python2.py. For solving this you need to create two new process like when forking a child (Look on processes concepts) and communicate with the in some way.

    – Iulian
    Nov 23 '18 at 19:41


















1















I have two python scripts that use two different cameras for a project I am working on and I am trying to run them both inside a different script or within each other, either way is fine.



import os

os.system('python 1.py')

os.system('python 2.py')


My problem however is that they don't run at the same time, I have to quit the first one for the next to open. I also tried doing it with bash as well with the & shell operator



python 1.py &
python 2.py &


And this does in fact make them both run however the issue is that they both run endlessly in the background and I need to close them rather easily. Any suggestion what I can do to avoid the issues with these implementations










share|improve this question























  • Because your python scripts are programs that never ends until user doesn't decide to close them or error raises your first statement blocks your calling program. So until you don't close python1.py obviously python will never go to execute the next statement that in your case is another endless program python2.py. For solving this you need to create two new process like when forking a child (Look on processes concepts) and communicate with the in some way.

    – Iulian
    Nov 23 '18 at 19:41














1












1








1








I have two python scripts that use two different cameras for a project I am working on and I am trying to run them both inside a different script or within each other, either way is fine.



import os

os.system('python 1.py')

os.system('python 2.py')


My problem however is that they don't run at the same time, I have to quit the first one for the next to open. I also tried doing it with bash as well with the & shell operator



python 1.py &
python 2.py &


And this does in fact make them both run however the issue is that they both run endlessly in the background and I need to close them rather easily. Any suggestion what I can do to avoid the issues with these implementations










share|improve this question














I have two python scripts that use two different cameras for a project I am working on and I am trying to run them both inside a different script or within each other, either way is fine.



import os

os.system('python 1.py')

os.system('python 2.py')


My problem however is that they don't run at the same time, I have to quit the first one for the next to open. I also tried doing it with bash as well with the & shell operator



python 1.py &
python 2.py &


And this does in fact make them both run however the issue is that they both run endlessly in the background and I need to close them rather easily. Any suggestion what I can do to avoid the issues with these implementations







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 19:25









acceptablesimple7acceptablesimple7

235




235













  • Because your python scripts are programs that never ends until user doesn't decide to close them or error raises your first statement blocks your calling program. So until you don't close python1.py obviously python will never go to execute the next statement that in your case is another endless program python2.py. For solving this you need to create two new process like when forking a child (Look on processes concepts) and communicate with the in some way.

    – Iulian
    Nov 23 '18 at 19:41



















  • Because your python scripts are programs that never ends until user doesn't decide to close them or error raises your first statement blocks your calling program. So until you don't close python1.py obviously python will never go to execute the next statement that in your case is another endless program python2.py. For solving this you need to create two new process like when forking a child (Look on processes concepts) and communicate with the in some way.

    – Iulian
    Nov 23 '18 at 19:41

















Because your python scripts are programs that never ends until user doesn't decide to close them or error raises your first statement blocks your calling program. So until you don't close python1.py obviously python will never go to execute the next statement that in your case is another endless program python2.py. For solving this you need to create two new process like when forking a child (Look on processes concepts) and communicate with the in some way.

– Iulian
Nov 23 '18 at 19:41





Because your python scripts are programs that never ends until user doesn't decide to close them or error raises your first statement blocks your calling program. So until you don't close python1.py obviously python will never go to execute the next statement that in your case is another endless program python2.py. For solving this you need to create two new process like when forking a child (Look on processes concepts) and communicate with the in some way.

– Iulian
Nov 23 '18 at 19:41












3 Answers
3






active

oldest

votes


















2














You could do it with multiprocessing



import os
import time
import psutil
from multiprocessing import Process

def run_program(cmd):
# Function that processes will run
os.system(cmd)

# Initiating Processes with desired arguments
program1 = Process(target=run_program, args=('python 1.py',))
program2 = Process(target=run_program, args=('python 2.py',))

# Start our processes simultaneously
program1.start()
program2.start()

def kill(proc_pid):
process = psutil.Process(proc_pid)
for proc in process.children(recursive=True):
proc.kill()
process.kill()

# Wait 5 seconds and kill first program
time.sleep(5)
kill(program1.pid)
program1.join()

# Wait another 1 second and kill second program
time.sleep(1)
kill(program2.pid)
program2.join()

# Print current status of our programs
print('1.py alive status: {}'.format(program1.is_alive()))
print('2.py alive status: {}'.format(program2.is_alive()))





share|improve this answer


























  • I don't think he wants to terminate the called scripts after some fixed time. It's more like execute this two scripts and wait for when they're closed so check last conditions and exit. So you need to wait for both of them tho terminate independently in which order the scripts are closed.

    – Iulian
    Nov 23 '18 at 20:00






  • 1





    He said that they run forever and he needs to be able to close them they both run endlessly in the background and I need to close them rather easily, so that's what my code do. If however you'd like to wait for them to finish just remove lines with terminate() function and keep join() that will wait for processes to finish.

    – Filip Młynarski
    Nov 23 '18 at 20:06











  • I edited this error while ago, try current code.

    – Filip Młynarski
    Nov 23 '18 at 20:19











  • I tried the current code and it launches both programs but its actually not terminating them with the timer

    – acceptablesimple7
    Nov 23 '18 at 20:28











  • I tested it with python programs that run infinite loop and had same problem. I fixed it using psutil library, you can try code now.

    – Filip Młynarski
    Nov 23 '18 at 20:48



















0














One possible method is to use systemd to control your process (i.e. treat them as daemons).



This is how I control my Python servers since they need to run in the background and be completely detached from the current tty so I can exit my connection to the machine and the continue processes continue. You can then also stop the server later using systemctl, as explained below.



Instructions:



Create a .service file and save it in /etc/systemd/system, with contents along the lines of:



[Unit]
Description=daemon one

[Service]
ExecStart=/path/to/1.py


and repeat with one going to 2.py.



Then you can use systemctl to control your daemons.



First reload all config files with:



systemctl daemon-reload


then start either of your daemons (where my_daemon.service is one of your unit files):



systemctl start my_daemon


it should now be running and you should find it in:



systemctl list-units


You can also check its status with:



systemctl status my_daemon


and stop/restart them with:



systemctl stop|restart my_daemon





share|improve this answer































    0














    Use subprocess.Popen. This will create a child process and return its pid.



    pid = Popen("python 1.py").pid


    And then check out these functions for communicating with the child process and checking if it is still running.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452027%2fissues-with-python-scripts-running-simultaneously%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You could do it with multiprocessing



      import os
      import time
      import psutil
      from multiprocessing import Process

      def run_program(cmd):
      # Function that processes will run
      os.system(cmd)

      # Initiating Processes with desired arguments
      program1 = Process(target=run_program, args=('python 1.py',))
      program2 = Process(target=run_program, args=('python 2.py',))

      # Start our processes simultaneously
      program1.start()
      program2.start()

      def kill(proc_pid):
      process = psutil.Process(proc_pid)
      for proc in process.children(recursive=True):
      proc.kill()
      process.kill()

      # Wait 5 seconds and kill first program
      time.sleep(5)
      kill(program1.pid)
      program1.join()

      # Wait another 1 second and kill second program
      time.sleep(1)
      kill(program2.pid)
      program2.join()

      # Print current status of our programs
      print('1.py alive status: {}'.format(program1.is_alive()))
      print('2.py alive status: {}'.format(program2.is_alive()))





      share|improve this answer


























      • I don't think he wants to terminate the called scripts after some fixed time. It's more like execute this two scripts and wait for when they're closed so check last conditions and exit. So you need to wait for both of them tho terminate independently in which order the scripts are closed.

        – Iulian
        Nov 23 '18 at 20:00






      • 1





        He said that they run forever and he needs to be able to close them they both run endlessly in the background and I need to close them rather easily, so that's what my code do. If however you'd like to wait for them to finish just remove lines with terminate() function and keep join() that will wait for processes to finish.

        – Filip Młynarski
        Nov 23 '18 at 20:06











      • I edited this error while ago, try current code.

        – Filip Młynarski
        Nov 23 '18 at 20:19











      • I tried the current code and it launches both programs but its actually not terminating them with the timer

        – acceptablesimple7
        Nov 23 '18 at 20:28











      • I tested it with python programs that run infinite loop and had same problem. I fixed it using psutil library, you can try code now.

        – Filip Młynarski
        Nov 23 '18 at 20:48
















      2














      You could do it with multiprocessing



      import os
      import time
      import psutil
      from multiprocessing import Process

      def run_program(cmd):
      # Function that processes will run
      os.system(cmd)

      # Initiating Processes with desired arguments
      program1 = Process(target=run_program, args=('python 1.py',))
      program2 = Process(target=run_program, args=('python 2.py',))

      # Start our processes simultaneously
      program1.start()
      program2.start()

      def kill(proc_pid):
      process = psutil.Process(proc_pid)
      for proc in process.children(recursive=True):
      proc.kill()
      process.kill()

      # Wait 5 seconds and kill first program
      time.sleep(5)
      kill(program1.pid)
      program1.join()

      # Wait another 1 second and kill second program
      time.sleep(1)
      kill(program2.pid)
      program2.join()

      # Print current status of our programs
      print('1.py alive status: {}'.format(program1.is_alive()))
      print('2.py alive status: {}'.format(program2.is_alive()))





      share|improve this answer


























      • I don't think he wants to terminate the called scripts after some fixed time. It's more like execute this two scripts and wait for when they're closed so check last conditions and exit. So you need to wait for both of them tho terminate independently in which order the scripts are closed.

        – Iulian
        Nov 23 '18 at 20:00






      • 1





        He said that they run forever and he needs to be able to close them they both run endlessly in the background and I need to close them rather easily, so that's what my code do. If however you'd like to wait for them to finish just remove lines with terminate() function and keep join() that will wait for processes to finish.

        – Filip Młynarski
        Nov 23 '18 at 20:06











      • I edited this error while ago, try current code.

        – Filip Młynarski
        Nov 23 '18 at 20:19











      • I tried the current code and it launches both programs but its actually not terminating them with the timer

        – acceptablesimple7
        Nov 23 '18 at 20:28











      • I tested it with python programs that run infinite loop and had same problem. I fixed it using psutil library, you can try code now.

        – Filip Młynarski
        Nov 23 '18 at 20:48














      2












      2








      2







      You could do it with multiprocessing



      import os
      import time
      import psutil
      from multiprocessing import Process

      def run_program(cmd):
      # Function that processes will run
      os.system(cmd)

      # Initiating Processes with desired arguments
      program1 = Process(target=run_program, args=('python 1.py',))
      program2 = Process(target=run_program, args=('python 2.py',))

      # Start our processes simultaneously
      program1.start()
      program2.start()

      def kill(proc_pid):
      process = psutil.Process(proc_pid)
      for proc in process.children(recursive=True):
      proc.kill()
      process.kill()

      # Wait 5 seconds and kill first program
      time.sleep(5)
      kill(program1.pid)
      program1.join()

      # Wait another 1 second and kill second program
      time.sleep(1)
      kill(program2.pid)
      program2.join()

      # Print current status of our programs
      print('1.py alive status: {}'.format(program1.is_alive()))
      print('2.py alive status: {}'.format(program2.is_alive()))





      share|improve this answer















      You could do it with multiprocessing



      import os
      import time
      import psutil
      from multiprocessing import Process

      def run_program(cmd):
      # Function that processes will run
      os.system(cmd)

      # Initiating Processes with desired arguments
      program1 = Process(target=run_program, args=('python 1.py',))
      program2 = Process(target=run_program, args=('python 2.py',))

      # Start our processes simultaneously
      program1.start()
      program2.start()

      def kill(proc_pid):
      process = psutil.Process(proc_pid)
      for proc in process.children(recursive=True):
      proc.kill()
      process.kill()

      # Wait 5 seconds and kill first program
      time.sleep(5)
      kill(program1.pid)
      program1.join()

      # Wait another 1 second and kill second program
      time.sleep(1)
      kill(program2.pid)
      program2.join()

      # Print current status of our programs
      print('1.py alive status: {}'.format(program1.is_alive()))
      print('2.py alive status: {}'.format(program2.is_alive()))






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 23 '18 at 20:48

























      answered Nov 23 '18 at 19:33









      Filip MłynarskiFilip Młynarski

      2,0441415




      2,0441415













      • I don't think he wants to terminate the called scripts after some fixed time. It's more like execute this two scripts and wait for when they're closed so check last conditions and exit. So you need to wait for both of them tho terminate independently in which order the scripts are closed.

        – Iulian
        Nov 23 '18 at 20:00






      • 1





        He said that they run forever and he needs to be able to close them they both run endlessly in the background and I need to close them rather easily, so that's what my code do. If however you'd like to wait for them to finish just remove lines with terminate() function and keep join() that will wait for processes to finish.

        – Filip Młynarski
        Nov 23 '18 at 20:06











      • I edited this error while ago, try current code.

        – Filip Młynarski
        Nov 23 '18 at 20:19











      • I tried the current code and it launches both programs but its actually not terminating them with the timer

        – acceptablesimple7
        Nov 23 '18 at 20:28











      • I tested it with python programs that run infinite loop and had same problem. I fixed it using psutil library, you can try code now.

        – Filip Młynarski
        Nov 23 '18 at 20:48



















      • I don't think he wants to terminate the called scripts after some fixed time. It's more like execute this two scripts and wait for when they're closed so check last conditions and exit. So you need to wait for both of them tho terminate independently in which order the scripts are closed.

        – Iulian
        Nov 23 '18 at 20:00






      • 1





        He said that they run forever and he needs to be able to close them they both run endlessly in the background and I need to close them rather easily, so that's what my code do. If however you'd like to wait for them to finish just remove lines with terminate() function and keep join() that will wait for processes to finish.

        – Filip Młynarski
        Nov 23 '18 at 20:06











      • I edited this error while ago, try current code.

        – Filip Młynarski
        Nov 23 '18 at 20:19











      • I tried the current code and it launches both programs but its actually not terminating them with the timer

        – acceptablesimple7
        Nov 23 '18 at 20:28











      • I tested it with python programs that run infinite loop and had same problem. I fixed it using psutil library, you can try code now.

        – Filip Młynarski
        Nov 23 '18 at 20:48

















      I don't think he wants to terminate the called scripts after some fixed time. It's more like execute this two scripts and wait for when they're closed so check last conditions and exit. So you need to wait for both of them tho terminate independently in which order the scripts are closed.

      – Iulian
      Nov 23 '18 at 20:00





      I don't think he wants to terminate the called scripts after some fixed time. It's more like execute this two scripts and wait for when they're closed so check last conditions and exit. So you need to wait for both of them tho terminate independently in which order the scripts are closed.

      – Iulian
      Nov 23 '18 at 20:00




      1




      1





      He said that they run forever and he needs to be able to close them they both run endlessly in the background and I need to close them rather easily, so that's what my code do. If however you'd like to wait for them to finish just remove lines with terminate() function and keep join() that will wait for processes to finish.

      – Filip Młynarski
      Nov 23 '18 at 20:06





      He said that they run forever and he needs to be able to close them they both run endlessly in the background and I need to close them rather easily, so that's what my code do. If however you'd like to wait for them to finish just remove lines with terminate() function and keep join() that will wait for processes to finish.

      – Filip Młynarski
      Nov 23 '18 at 20:06













      I edited this error while ago, try current code.

      – Filip Młynarski
      Nov 23 '18 at 20:19





      I edited this error while ago, try current code.

      – Filip Młynarski
      Nov 23 '18 at 20:19













      I tried the current code and it launches both programs but its actually not terminating them with the timer

      – acceptablesimple7
      Nov 23 '18 at 20:28





      I tried the current code and it launches both programs but its actually not terminating them with the timer

      – acceptablesimple7
      Nov 23 '18 at 20:28













      I tested it with python programs that run infinite loop and had same problem. I fixed it using psutil library, you can try code now.

      – Filip Młynarski
      Nov 23 '18 at 20:48





      I tested it with python programs that run infinite loop and had same problem. I fixed it using psutil library, you can try code now.

      – Filip Młynarski
      Nov 23 '18 at 20:48













      0














      One possible method is to use systemd to control your process (i.e. treat them as daemons).



      This is how I control my Python servers since they need to run in the background and be completely detached from the current tty so I can exit my connection to the machine and the continue processes continue. You can then also stop the server later using systemctl, as explained below.



      Instructions:



      Create a .service file and save it in /etc/systemd/system, with contents along the lines of:



      [Unit]
      Description=daemon one

      [Service]
      ExecStart=/path/to/1.py


      and repeat with one going to 2.py.



      Then you can use systemctl to control your daemons.



      First reload all config files with:



      systemctl daemon-reload


      then start either of your daemons (where my_daemon.service is one of your unit files):



      systemctl start my_daemon


      it should now be running and you should find it in:



      systemctl list-units


      You can also check its status with:



      systemctl status my_daemon


      and stop/restart them with:



      systemctl stop|restart my_daemon





      share|improve this answer




























        0














        One possible method is to use systemd to control your process (i.e. treat them as daemons).



        This is how I control my Python servers since they need to run in the background and be completely detached from the current tty so I can exit my connection to the machine and the continue processes continue. You can then also stop the server later using systemctl, as explained below.



        Instructions:



        Create a .service file and save it in /etc/systemd/system, with contents along the lines of:



        [Unit]
        Description=daemon one

        [Service]
        ExecStart=/path/to/1.py


        and repeat with one going to 2.py.



        Then you can use systemctl to control your daemons.



        First reload all config files with:



        systemctl daemon-reload


        then start either of your daemons (where my_daemon.service is one of your unit files):



        systemctl start my_daemon


        it should now be running and you should find it in:



        systemctl list-units


        You can also check its status with:



        systemctl status my_daemon


        and stop/restart them with:



        systemctl stop|restart my_daemon





        share|improve this answer


























          0












          0








          0







          One possible method is to use systemd to control your process (i.e. treat them as daemons).



          This is how I control my Python servers since they need to run in the background and be completely detached from the current tty so I can exit my connection to the machine and the continue processes continue. You can then also stop the server later using systemctl, as explained below.



          Instructions:



          Create a .service file and save it in /etc/systemd/system, with contents along the lines of:



          [Unit]
          Description=daemon one

          [Service]
          ExecStart=/path/to/1.py


          and repeat with one going to 2.py.



          Then you can use systemctl to control your daemons.



          First reload all config files with:



          systemctl daemon-reload


          then start either of your daemons (where my_daemon.service is one of your unit files):



          systemctl start my_daemon


          it should now be running and you should find it in:



          systemctl list-units


          You can also check its status with:



          systemctl status my_daemon


          and stop/restart them with:



          systemctl stop|restart my_daemon





          share|improve this answer













          One possible method is to use systemd to control your process (i.e. treat them as daemons).



          This is how I control my Python servers since they need to run in the background and be completely detached from the current tty so I can exit my connection to the machine and the continue processes continue. You can then also stop the server later using systemctl, as explained below.



          Instructions:



          Create a .service file and save it in /etc/systemd/system, with contents along the lines of:



          [Unit]
          Description=daemon one

          [Service]
          ExecStart=/path/to/1.py


          and repeat with one going to 2.py.



          Then you can use systemctl to control your daemons.



          First reload all config files with:



          systemctl daemon-reload


          then start either of your daemons (where my_daemon.service is one of your unit files):



          systemctl start my_daemon


          it should now be running and you should find it in:



          systemctl list-units


          You can also check its status with:



          systemctl status my_daemon


          and stop/restart them with:



          systemctl stop|restart my_daemon






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 19:33









          Joe IddonJoe Iddon

          15.5k31741




          15.5k31741























              0














              Use subprocess.Popen. This will create a child process and return its pid.



              pid = Popen("python 1.py").pid


              And then check out these functions for communicating with the child process and checking if it is still running.






              share|improve this answer




























                0














                Use subprocess.Popen. This will create a child process and return its pid.



                pid = Popen("python 1.py").pid


                And then check out these functions for communicating with the child process and checking if it is still running.






                share|improve this answer


























                  0












                  0








                  0







                  Use subprocess.Popen. This will create a child process and return its pid.



                  pid = Popen("python 1.py").pid


                  And then check out these functions for communicating with the child process and checking if it is still running.






                  share|improve this answer













                  Use subprocess.Popen. This will create a child process and return its pid.



                  pid = Popen("python 1.py").pid


                  And then check out these functions for communicating with the child process and checking if it is still running.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 19:36









                  Mr. MeMr. Me

                  3,29012337




                  3,29012337






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452027%2fissues-with-python-scripts-running-simultaneously%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini