<bdo id='6DzeN'></bdo><ul id='6DzeN'></ul>
    1. <tfoot id='6DzeN'></tfoot>

      <small id='6DzeN'></small><noframes id='6DzeN'>

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

        用于 Windows 的 python os.mkfifo()

        时间:2023-07-22

          <bdo id='Ge7I5'></bdo><ul id='Ge7I5'></ul>
            <legend id='Ge7I5'><style id='Ge7I5'><dir id='Ge7I5'><q id='Ge7I5'></q></dir></style></legend>
          • <tfoot id='Ge7I5'></tfoot>

            1. <small id='Ge7I5'></small><noframes id='Ge7I5'>

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

                • 本文介绍了用于 Windows 的 python os.mkfifo()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  短版(如果你能回答短版它对我有用,其余的主要是为了其他有类似任务的人的利益):

                  Short version (if you can answer the short version it does the job for me, the rest is mainly for the benefit of other people with a similar task):

                  在 Windows 的 python 中,我想创建 2 个文件对象,附加到同一个文件(它不必是硬盘驱动器上的实际文件),一个用于读取,一个用于写入,这样如果读取端尝试读取它永远不会得到 EOF(它只会阻塞直到写入某些内容).我认为在 linux os.mkfifo() 中可以完成这项工作,但在 Windows 中它不存在.可以做什么?(我必须使用文件对象).

                  In python in Windows, I want to create 2 file objects, attached to the same file (it doesn't have to be an actual file on the hard-drive), one for reading and one for writing, such that if the reading end tries to read it will never get EOF (it will just block until something is written). I think in linux os.mkfifo() would do the job, but in Windows it doesn't exist. What can be done? (I must use file-objects).

                  一些额外的细节:我有一个 python 模块(不是我写的),它通过标准输入和标准输出(使用 raw_input() 和打印)玩某个游戏.我也有一个 Windows 可执行文件通过标准输入和标准输出玩同样的游戏.我想让他们互相对战,并记录他们的所有交流.

                  Some extra details: I have a python module (not written by me) that plays a certain game through stdin and stdout (using raw_input() and print). I also have a Windows executable playing the same game, through stdin and stdout as well. I want to make them play one against the other, and log all their communication.

                  这是我可以编写的代码(get_fifo() 函数没有实现,因为我不知道在 Windows 中这样做):

                  Here's the code I can write (the get_fifo() function is not implemented, because that's what I don't know to do it Windows):

                  class Pusher(Thread):
                          def __init__(self, source, dest, p1, name):
                                  Thread.__init__(self)
                                  self.source = source
                                  self.dest = dest
                                  self.name = name
                                  self.p1 = p1
                  
                          def run(self):
                                  while (self.p1.poll()==None) and
                                        (not self.source.closed) and (not self.source.closed):
                                          line = self.source.readline()
                                          logging.info('%s: %s' % (self.name, line[:-1]))
                                          self.dest.write(line)
                                          self.dest.flush()
                  
                  
                  exe_to_pythonmodule_reader, exe_to_pythonmodule_writer =
                                            get_fifo()
                  pythonmodule_to_exe_reader, pythonmodule_to_exe_writer =
                                            get_fifo()
                  
                  p1 = subprocess.Popen(exe, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                  
                  old_stdin = sys.stdin
                  old_stdout = sys.stdout
                  
                  sys.stdin = exe_to_pythonmodule_reader
                  sys.stdout = pythonmodule_to_exe_writer
                  
                  push1 = Pusher(p1.stdout, exe_to_pythonmodule_writer, p1, '1')
                  push2 = Pusher(pythonmodule_to_exe_reader, p1.stdin, p1, '2')
                  
                  push1.start()
                  push2.start()
                  ret = pythonmodule.play()
                  sys.stdin = old_stdin
                  sys.stdout = old_stdout
                  

                  推荐答案

                  按照上面两个答案,不小心碰到了答案.os.pipe() 完成了这项工作.谢谢你的回答.

                  Following the two answers above, I accidentally bumped into the answer. os.pipe() does the job. Thank you for your answers.

                  我发布完整的代码以防其他人正在寻找这个:

                  I'm posting the complete code in case someone else is looking for this:

                  import subprocess
                  from threading import Thread
                  import time
                  import sys
                  import logging
                  import tempfile
                  import os
                  
                  import game_playing_module
                  
                  class Pusher(Thread):
                      def __init__(self, source, dest, proc, name):
                          Thread.__init__(self)
                          self.source = source
                          self.dest = dest
                          self.name = name
                          self.proc = proc
                  
                      def run(self):
                          while (self.proc.poll()==None) and
                                (not self.source.closed) and (not self.dest.closed):
                              line = self.source.readline()
                              logging.info('%s: %s' % (self.name, line[:-1]))
                              self.dest.write(line)
                              self.dest.flush()
                  
                  def get_reader_writer():
                      fd_read, fd_write = os.pipe()
                      return os.fdopen(fd_read, 'r'), os.fdopen(fd_write, 'w')
                  
                  def connect(exe):
                      logging.basicConfig(level=logging.DEBUG,
                                          format='%(message)s',
                                          filename=LOG_FILE_NAME,
                                          filemode='w')
                  
                      program_to_grader_reader, program_to_grader_writer =
                                                get_reader_writer()
                  
                      grader_to_program_reader, grader_to_program_writer =
                                                get_reader_writer()
                  
                      p1 = subprocess.Popen(exe, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)        
                  
                      old_stdin = sys.stdin
                      old_stdout = sys.stdout
                  
                      sys.stdin = program_to_grader_reader
                      sys.stdout = grader_to_program_writer
                  
                      push1 = Pusher(p1.stdout, program_to_grader_writer, p1, '1')
                      push2 = Pusher(grader_to_program_reader, p1.stdin, p1, '2')
                  
                      push1.start()
                      push2.start()
                  
                      game_playing_module.play()
                  
                      sys.stdin = old_stdin
                      sys.stdout = old_stdout
                  
                      fil = file(LOG_FILE, 'r')
                      data = fil.read()
                      fil.close()
                      return data
                  
                  if __name__=='__main__':
                      if len(sys.argv) != 2:
                          print 'Usage: connect.py exe'
                          print sys.argv
                          exit()
                      print sys.argv
                      print connect(sys.argv[1])
                  

                  这篇关于用于 Windows 的 python os.mkfifo()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 subprocess.call 中使用大于运算符 下一篇:Python 子进程 Popen:为什么会出现“ls *.txt"?不行?

                  相关文章

                    <bdo id='VOf3x'></bdo><ul id='VOf3x'></ul>

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