python中的子处理

时间:2017-03-11 20:57:27

标签: python subprocess

我想从父代码调用无限循环脚本。

from subprocess import Popen
import subprocess
import os
import sys
proc=subprocess.Popen([sys.executable, os.getcwd()+'/A.py'],shell=False, stdout=subprocess.PIPE)
print(proc.communicate())
print('Hi')

我的脚本是:

import time

def main():
    while True:
        print('HI2')
        time.sleep(3)
if __name__ == '__main__':
    main()`

但它不起作用,Popen会阻止脚本。我的意思是为什么print('Hi')语法不起作用。

1 个答案:

答案 0 :(得分:1)

proc等待out, err = proc.communicate() 完成。然后它返回子进程的输出。

在这个示例中,out是打印在stdout上的输出,错误是stderr上的输出:

communicate()

如果您不需要输出,请跳过breed [people p] breed [party1 p1] breed [party2 p2] breed [party3 p3] party1-own [vote] party2-own [vote] party3-own [vote] to setup clear-all setup-patches setup-people setup-parties reset-ticks end to setup-patches ask patches [ ifelse pxcor >= 4 and pycor >= 6 [set pcolor white] [set pcolor brown] ] end to setup-people set-default-shape people "person" create-people number-of-people ask people [setxy random-float -16 random-float -16] end to setup-parties set-default-shape party1 "person" set-default-shape party2 "person" set-default-shape party3 "person" create-party1 1 create-party2 1 create-party3 1 ask party1 [setxy 15 -1] ask party2 [setxy 15 -3] ask party3 [setxy 15 -5] ask party1 [set color blue] ask party2 [set color green] ask party3 [set color yellow] end to go start-voting ask party1 [ if (vote) >= 100 [stop] ] ask party2 [ if (vote) >= 100 [stop] ] ask party3 [ if (vote) >= 100 [stop] ] tick end to start-voting let x random 3 ifelse x = 2 [ask party3 [set vote vote + 1]] [ ifelse x = 1 [ask party2 [set vote vote + 1]] [ask party1 [set vote vote + 1]] ] ifelse show-votes? [ ask party1 [set label vote] ask party2 [set label vote] ask party3 [set label vote] ] [ ask party1 [set label ""] ask party2 [set label ""] ask party3 [set label ""] ] end