如何使此与Windows兼容?

2024-04-26 12:56:28 发布

您现在位置:Python中文网/ 问答频道 /正文

再见,斯塔克弗!在

在将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

纵火的问题是:

  • 为什么不在windows版本中使用select()?这样不行吗?在
  • 如果您不在那里使用select(),那么我如何实现select()提供的必要超时(在这里显然不能这样工作)

求你了,帮帮我!在


Tags: 脚本readserver进程windowsstderrstdinstdout
1条回答
网友
1楼 · 发布于 2024-04-26 12:56:28

我想你不能在管道上使用select()。 在其中一个项目中,我把一个linux应用程序移植到Windows上,我也错过了这一点,不得不重写整个逻辑。在

相关问题 更多 >