Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法通过';标准偏差';作为python中另一个进程的参数?_Python_Multiprocessing_Stdin - Fatal编程技术网

有没有办法通过';标准偏差';作为python中另一个进程的参数?

有没有办法通过';标准偏差';作为python中另一个进程的参数?,python,multiprocessing,stdin,Python,Multiprocessing,Stdin,我正在尝试创建一个脚本,该脚本使用python的多处理模块。脚本(我们称之为myscript.py)将从另一个带有管道的脚本获取输入 假设我这样调用脚本 $ python writer.py | python myscript.py //myscript.py def get_input(): while True: text = sys.stdin.readline() print "hello " + text time.sleep

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

假设我这样调用脚本

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

def do_more_things():
    while True:
        #// some code here
        time.sleep(60*5)

if __name__ == '__main__':        
    p1 = Process(target=get_input, args=())
    p1.start()

    do_more_things()
这是代码

// 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()
这显然不起作用,因为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()
但我遇到了这个错误

Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "in.py", line 12, in get_input
    text = temp.readline()
ValueError: I/O operation on closed file
所以,我猜main的stdin文件关闭了,我无法读取它。此时,如何将main的stdin文件传递给另一个进程?如果无法通过stdin,如何从另一个进程使用main的stdin

更新: 好的,我需要澄清我的问题,因为人们认为使用多处理并不是真的必要。 请考虑<代码> MyScript .Py < /代码>;p>
$ python writer.py | python myscript.py 
//myscript.py
def get_input():
    while True:
        text = sys.stdin.readline()
        print "hello " + text
        time.sleep(3)

def do_more_things():
    while True:
        #// some code here
        time.sleep(60*5)

if __name__ == '__main__':        
    p1 = Process(target=get_input, args=())
    p1.start()

    do_more_things()
所以,我确实需要与主函数(或其他子进程)并行运行get_input()函数。 对不起,我的英语很好,我想我在这个问题上不太清楚。如果你们能告诉我是否可以在另一个进程中使用主进程STDIN对象,我将不胜感激


提前感谢。

使用多处理模块创建的每个新进程都有自己的PID,因此它有自己的标准输入设备和输出设备,即使它们都在写入同一个终端,因此需要锁

您已经通过将内容分成两个脚本创建了两个进程,并使用get_input()创建了第三个进程。如果是线程而不是进程,get_input可以读取标准输入。然后,不需要在读卡器中具有睡眠功能

## reader.py
from threading import Thread
import sys

def get_input():
    text = sys.stdin.readline()
    while len(text) != 0:
        print 'hello ' + text
        text = sys.stdin.readline()

if __name__ == '__main__':
    thread = Thread(target=get_input)
    thread.start()
    thread.join()

这只是一个部分的答案——因为我不清楚问题的后续部分

首先,您可以说您希望调用脚本:

$ python writer.py | python myscript.py 
若要这样做,writer需要写入标准输出,myscript需要读取标准输入。第二个脚本如下所示:

def get_input():
    while True:
        text = sys.stdin.readline()
        print "hello " + text
        time.sleep(3)
if __name__ == '__main__':    
    get_input()
不需要多处理。处理对象。。。您已经从命令行启动了两个进程,并且使用shell将它们与(匿名)管道(即“|”字符)连接起来,该管道将第一个脚本的标准输出连接到第二个脚本的标准输入

流程对象的要点是从第一个流程开始管理第二个流程的启动。你需要定义一个过程;然后启动它-然后您可能想等到它终止后再退出第一个进程。。。(在p1.start()之后调用p1.join()就足够了)


如果希望在python控制下的一对进程之间进行通信,可能需要使用对象来实现。然后,您可以通过读取和写入管道对象(而不是标准输入和标准输出)来轻松地在初始进程和从属派生进程之间进行通信。如果您真的想重新定向标准输入和标准输出,可能需要使用低级文件描述符和/或重写/替换sys.stdin和sys.stdout对象。。。但是,我怀疑,您可能不想(或不需要)这样做。

要阅读管道输入使用:

myscript.py

import fileinput

if __name__ == '__main__':
    for line in fileinput.input():
        #do stuff here
        process_line(line)

最简单的方法是在父进程中交换
get\u input()
do\u more\u things()
即读取
sys.stdin

def get_input(stdin):
    for line in iter(stdin.readline, ''):
        print("hello", line, end='')
    stdin.close()

if __name__ == '__main__':
    p1 = mp.Process(target=do_more_things)
    p1.start()
    get_input(sys.stdin)
下一个最好的方法是使用
线程()
而不是
进程()
来执行
获取输入()

如果上述方法无效,您可以尝试
os.dup()


您可以使用此处找到的用于读取的语法写入stdin,但您可以使用相同的对象为什么
myscript.py
创建子流程?为什么它不直接从sys.stdin读取?如果从
sys.stdin
读取,shell脚本将工作得很好。为什么要创建子进程?嗨,S.洛特,这只是我问题的一个原型,myscript.py包含另一个进程,它有另一个无限循环,像守护进程一样运行,所以我无法在myscript主进程中读取它,因为writer.py也是无限的,需要作为守护进程运行@JohanLundberg,谢谢你的建议,我会查一下。这不就是
(python writer.py | python myscript.py)&python dou more_things.py
?我不明白为什么这不是三个单独的Python程序。两个组成一个简单的管道,第三个完全不相关。类似于:虽然我只需要第三个,但其他解决方案都很有效。感谢您花时间全面帮助我们。