Running multiple Python scripts

后端 未结 3 904
梦毁少年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
    

提交回复
热议问题