Running multiple Python scripts

后端 未结 3 876
梦毁少年i
梦毁少年i 2021-01-22 17:20

I would like to create a simple Python program that will concurrently execute 2 independent scripts. For now, the two scripts just print a sequence of numbers but my intention i

相关标签:
3条回答
  • 2021-01-22 17:35

    As wanderlust mentioned, why do you want to do it this way and not via linux command line?

    Otherwise, the solution you post is doing what it is meant to, i.e, you are doing this at the command line:

    screen ./thread1.py
    screen ./thread2.py
    

    This will open a screen session and run the program and output within this screen session, such that you will not see the output on your terminal directly. To trouble shoot your output, just execute the scripts without the screen call:

    import subprocess
    
    subprocess.Popen(['./thread1.py'])
    subprocess.Popen(['./thread2.py'])
    

    Content of thread1.py:

    #!/usr/bin/env python
    def countToTen():
      for i in range(10):
      print i
    
    countToTen()
    

    Content of thread2.py:

    #!/usr/bin/env python
    def countToHundreds():
    for i in range(10):
      print i*100
    
    countToHundreds()
    

    Then don't forget to do this on the command line:

    chmod u+x thread*.py
    
    0 讨论(0)
  • 2021-01-22 17:37

    You can also just open several Command Prompt windows to run several Python programs at once - just run one in each of them:

    In each Command Prompt window, go to the correct directory (such as C:/Python27) and then type 'python YourCodeNo1.py' in one Command Prompt window, 'python YourCodeNo2.py' in the next one ect. .

    I'm currently running 3 codes at one time in this way, without slowing any of them down.

    0 讨论(0)
  • 2021-01-22 17:38

    Use supervisord

    supervisord is process control system just for the purpose of running multiple command line scripts.

    It features:

    • multiple controlled processes
    • autorestarting failed runs
    • log stdout and stderr output
    • starting scripts in order (using priority)
    • command line utility to view latest log output, stop, start, restart the processes

    This solution works only on *nix based systems, it is not available on Windows.

    0 讨论(0)
提交回复
热议问题