相关疑难解决方法(0)

你怎么读斯坦丁?

我正在尝试做一些代码高尔夫挑战,但它们都需要输入stdin.我如何在Python中获得它?

python stdin

1389
推荐指数
18
解决办法
150万
查看次数

有没有办法将'stdin'作为参数传递给python中的另一个进程?

我正在尝试创建一个使用python的多处理模块的脚本.脚本(让我们称之为myscript.py)将从另一个带有管道的脚本获取输入.

假设我像这样调用脚本;

$ python writer.py | python myscript.py 
Run Code Online (Sandbox Code Playgroud)

这是代码;

// writer.py
import time, sys

def main():
    while True:
        print "test"
        sys.stdout.flush()
        time.sleep(1)

main()

//myscript.py
def get_input():
    while True:
        text = sys.stdin.readline()
        print "hello " + text
        time.sleep(3)

if __name__ == '__main__':        
    p1 = Process(target=get_input, args=())
    p1.start()
Run Code Online (Sandbox Code Playgroud)

这显然不起作用,因为sys.stdin对象对于主进程和p1是不同的.所以我试过这个解决它,

//myscript.py
def get_input(temp):
    while True:
        text = temp.readline()
        print "hello " + text
        time.sleep(3)

if __name__ == '__main__':        
    p1 = Process(target=get_input, args=(sys.stdin,))
    p1.start()
Run Code Online (Sandbox Code Playgroud)

但我遇到了这个错误;

Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", …
Run Code Online (Sandbox Code Playgroud)

python stdin multiprocessing

16
推荐指数
1
解决办法
7190
查看次数

标签 统计

python ×2

stdin ×2

multiprocessing ×1