短文(如果你能够回答我的工作的短文,其余部分主要是为了其他任务相似的人):
在Windows的陈词中,我想创建2个附于同一档案的档案物体(它不一定是硬拷贝的实际档案),1个是阅读的,1个是书面的,这样,如果阅读的结尾试图读到它,就永远不会获得EOF(在撰写文章之前,它就将受到阻碍)。 我认为,在中枢轴中,工作要做,但在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.
这里可以写成的代码I(get_fifo()
功能没有执行,因为我不知道做的是视窗:
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