再见,斯塔克弗!在
在将Python脚本移植到Windows时,我遇到了一个小(大)问题。最棘手的是,我必须启动一个进程,并将其所有流重定向到管道中,然后在脚本中对这些管道进行读写操作。在
对于Linux,这只是小菜一碟:
server_startcmd = [
"java",
"-Xmx%s" % self.java_heapmax,
"-Xms%s" % self.java_heapmin,
"-jar",
server_jar,
"nogui"
]
server = Popen(server_startcmd, stdout = PIPE,
stderr = PIPE,
stdin = PIPE)
outputs = [
server_socket, # A listener socket that has been setup before
server.stderr,
server.stdout,
sys.stdin # Because I also have to read and process this.
]
clients = []
while True:
read_ready, write_ready, except_ready = select.select(outputs, [], [], 1.0)
if read_ready == []:
perform_idle_command() # important step
else:
for s in read_ready:
if s == sys.stdin:
# Do stdin stuff
elif s == server_socket:
# Accept client and add it to 'clients'
elif s in clients:
# Got data from one of the clients
服务器套接字、脚本的stdin和子进程的输出通道(以及输入通道,我的脚本将写入该通道,尽管该通道不在select()列表中),这三种方式的交替是脚本最重要的部分。在
我知道对于Windows,win32api模块中有win32pipe。问题是找到这个API的资源非常困难,而我发现的并没有真正的帮助。在
如何利用这个win32pipe模块来做我想做的事?我有一些资料显示它在不同但相似的情况下使用,但这让我很困惑:
^{pr2}$stdout
这是用win32pipe.popen4(...)
启动的子进程的组合stdout和stderr
纵火的问题是:
select()
?这样不行吗?在select()
,那么我如何实现select()
提供的必要超时(在这里显然不能这样工作)求你了,帮帮我!在
我想你不能在管道上使用select()。 在其中一个项目中,我把一个linux应用程序移植到Windows上,我也错过了这一点,不得不重写整个逻辑。在
相关问题 更多 >
编程相关推荐