問題描述
我正在嘗試創(chuàng)建一個(gè)使用 python 的多處理模塊的腳本.該腳本(我們稱之為 myscript.py)將從另一個(gè)帶有管道的腳本中獲取輸入.
I'm trying to create a script which is using multiprocessing module with python. The script (lets call it myscript.py) will get the input from another script with pipe.
假設(shè)我這樣調(diào)用腳本;
$ python writer.py | python myscript.py
這里是代碼;
// 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()
這顯然不起作用,因?yàn)橹鬟M(jìn)程和 p1 的 sys.stdin 對(duì)象不同.所以我嘗試了這個(gè)來解決它,
this is clearly not working, since the sys.stdin objects are different for main process and p1. So I have tried this to solve it,
//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()
但是我遇到了這個(gè)錯(cuò)誤;
but I come across with this error;
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 文件已關(guān)閉,我無法從中讀取.在這個(gè)結(jié)合處,我如何將 main 的 stdin 文件傳遞??給另一個(gè)進(jìn)程?如果無法通過標(biāo)準(zhǔn)輸入,我該如何使用來自另一個(gè)進(jìn)程的 main 標(biāo)準(zhǔn)輸入?
So, I guess that main's stdin file closed and I can't read from it. At this conjunction, how can I pass main's stdin file to another process? If passing stdin is not possible, how can I use main's stdin from another process?
更新:好的,我需要澄清我的問題,因?yàn)槿藗冋J(rèn)為使用多處理并不是真正必要的.像這樣考慮 myscript.py
;
update:
Okay, I need to clarify my question since people think using multiprocessing is not really necessary.
consider myscript.py
like this;
//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() 函數(shù)與 main 函數(shù)(或其他子進(jìn)程)并行運(yùn)行.很抱歉發(fā)生沖突,我的英語不錯(cuò),我想我對(duì)這個(gè)問題不太清楚.如果你們能告訴我我是否可以在另一個(gè)進(jìn)程中使用主進(jìn)程 STDIN 對(duì)象,我將不勝感激.
so, I really need to run get_input() function parallelly with main function (or other sub processes). Sorry for the conflicts, I have a decent English, and I guess I couldn't be clear on this question. I would appreciate if you guys can tell me if i can use the main processes STDIN object in another process.
提前致謝.
推薦答案
最簡(jiǎn)單的就是交換get_input()
和do_more_things()
,即讀取sys.stdin
在父進(jìn)程中:
The simplest thing is to swap get_input()
and do_more_things()
i.e., read sys.stdin
in the parent process:
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)
下一個(gè)最好的方法是在 get_input()
中使用 Thread()
而不是 Process()
:
The next best thing is to use a Thread()
instead of a Process()
for get_input()
:
if __name__ == '__main__':
t = Thread(target=get_input, args=(sys.stdin,))
t.start()
do_more_things()
如果上述方法沒有幫助你可以嘗試 os.dup()
:
If the above doesn't help you could try os.dup()
:
newstdin = os.fdopen(os.dup(sys.stdin.fileno()))
try:
p = Process(target=get_input, args=(newstdin,))
p.start()
finally:
newstdin.close() # close in the parent
do_more_things()
這篇關(guān)于有沒有辦法將'stdin'作為參數(shù)傳遞給python中的另一個(gè)進(jìn)程?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!