• <legend id='Z7Ar7'><style id='Z7Ar7'><dir id='Z7Ar7'><q id='Z7Ar7'></q></dir></style></legend>
    • <bdo id='Z7Ar7'></bdo><ul id='Z7Ar7'></ul>
    <tfoot id='Z7Ar7'></tfoot>
    <i id='Z7Ar7'><tr id='Z7Ar7'><dt id='Z7Ar7'><q id='Z7Ar7'><span id='Z7Ar7'><b id='Z7Ar7'><form id='Z7Ar7'><ins id='Z7Ar7'></ins><ul id='Z7Ar7'></ul><sub id='Z7Ar7'></sub></form><legend id='Z7Ar7'></legend><bdo id='Z7Ar7'><pre id='Z7Ar7'><center id='Z7Ar7'></center></pre></bdo></b><th id='Z7Ar7'></th></span></q></dt></tr></i><div id='Z7Ar7'><tfoot id='Z7Ar7'></tfoot><dl id='Z7Ar7'><fieldset id='Z7Ar7'></fieldset></dl></div>

    <small id='Z7Ar7'></small><noframes id='Z7Ar7'>

        有没有办法将'stdin'作为参数传递给python中

        时间:2023-05-27
            <bdo id='aUDp6'></bdo><ul id='aUDp6'></ul>

            • <legend id='aUDp6'><style id='aUDp6'><dir id='aUDp6'><q id='aUDp6'></q></dir></style></legend>
                  <tbody id='aUDp6'></tbody>

                <small id='aUDp6'></small><noframes id='aUDp6'>

              • <i id='aUDp6'><tr id='aUDp6'><dt id='aUDp6'><q id='aUDp6'><span id='aUDp6'><b id='aUDp6'><form id='aUDp6'><ins id='aUDp6'></ins><ul id='aUDp6'></ul><sub id='aUDp6'></sub></form><legend id='aUDp6'></legend><bdo id='aUDp6'><pre id='aUDp6'><center id='aUDp6'></center></pre></bdo></b><th id='aUDp6'></th></span></q></dt></tr></i><div id='aUDp6'><tfoot id='aUDp6'></tfoot><dl id='aUDp6'><fieldset id='aUDp6'></fieldset></dl></div>
                <tfoot id='aUDp6'></tfoot>

                  本文介绍了有没有办法将'stdin'作为参数传递给python中的另一个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

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

                  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.

                  假设我这样调用脚本;

                  $ 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()
                  

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

                  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()
                  

                  但是我遇到了这个错误;

                  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 文件已关闭,我无法从中读取.在这个结合处,我如何将 main 的 stdin 文件传递​​给另一个进程?如果无法通过标准输入,我该如何使用来自另一个进程的 main 标准输入?

                  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?

                  更新:好的,我需要澄清我的问题,因为人们认为使用多处理并不是真正必要的.像这样考虑 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() 函数与 main 函数(或其他子进程)并行运行.很抱歉发生冲突,我的英语不错,我想我对这个问题不太清楚.如果你们能告诉我我是否可以在另一个进程中使用主进程 STDIN 对象,我将不胜感激.

                  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.

                  提前致谢.

                  推荐答案

                  最简单的就是交换get_input()do_more_things(),即读取sys.stdin 在父进程中:

                  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)
                  

                  下一个最好的方法是在 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()
                  

                  这篇关于有没有办法将'stdin'作为参数传递给python中的另一个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用多处理写入文件 下一篇:Python多处理 - 进程完成后如何释放内存?

                  相关文章

                  最新文章

                1. <i id='FL3hn'><tr id='FL3hn'><dt id='FL3hn'><q id='FL3hn'><span id='FL3hn'><b id='FL3hn'><form id='FL3hn'><ins id='FL3hn'></ins><ul id='FL3hn'></ul><sub id='FL3hn'></sub></form><legend id='FL3hn'></legend><bdo id='FL3hn'><pre id='FL3hn'><center id='FL3hn'></center></pre></bdo></b><th id='FL3hn'></th></span></q></dt></tr></i><div id='FL3hn'><tfoot id='FL3hn'></tfoot><dl id='FL3hn'><fieldset id='FL3hn'></fieldset></dl></div>

                      <bdo id='FL3hn'></bdo><ul id='FL3hn'></ul>
                    1. <legend id='FL3hn'><style id='FL3hn'><dir id='FL3hn'><q id='FL3hn'></q></dir></style></legend>
                    2. <tfoot id='FL3hn'></tfoot>

                    3. <small id='FL3hn'></small><noframes id='FL3hn'>