Send value of variable that is in a loop from one script to another script

前端 未结 2 1283
遥遥无期
遥遥无期 2021-01-27 08:22

I\'ve been fiddling with sending a variable from one script to another and have done some basic things pretty easily, but I have gotten stuck and was hoping to get some input.

2条回答
  •  终归单人心
    2021-01-27 08:57

    I finally got it to work. Thanks to going back and forth with Vadim, I was able to learn how some of this stuff works. Below is what my code is.

    script1.py:

    import sys, time
    x=0
    while x<10:
        print(x)
        sys.stdout.flush()
        x = x+1
        time.sleep(1)
    

    script2.py:

    import subprocess
    
    proc = subprocess.Popen('python script1.py', 
                            shell=True,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            )
    while True:
        output = proc.stdout.readline()
        if output == '':
            break
        else:
            print output.rstrip()
    

    Output:

    0 #waits one second
    1 #waits one second
    2 #waits one second
    .
    .
    .
    8 #waits one second
    9 #waits one second
    

提交回复
热议问题