【发布时间】:2016-06-29 03:38:42
【问题描述】:
我有两个 Python 脚本。
sub.py 代码:
import time
import subprocess as sub
while 1:
value=input("Input some text or number") # it is example, and I don't care about if it is number-input or text-raw_input, just input something
proces=sub.Popen(['sudo', 'python', '/home/pi/second.py'],stdin=sub.PIPE)
proces.stdin.write(value)
second.py 代码:
import sys
while 1:
from_sub=sys.stdin()#or sys.stdout() I dont remember...
list_args.append(from_sub) # I dont know if syntax is ok, but it doesn't matter
for i in list_arg:
print i
首先我执行 sub.py,然后输入一些内容,然后 second.py 文件将执行并打印我输入的所有内容,并一次又一次地打印...
问题是我不想打开新进程。应该只有一个过程。可能吗?
把手给我:)
【问题讨论】:
-
最好使用python命名约定,
while True而不是while 1 -
在 Python 2.x 中,使用
while 1而不是while True会更快,因为True不是关键字而是需要解释的全局变量,所以如果您需要最大速度 @ 987654328@ 击败while True。在 Python 3.x 中,True|False成为关键字,因此生成的字节码是等效的。 -
您的代码不是有效的 Python,indetion 使用了不同数量的空格。
标签: python subprocess popen