2010-08-13 60 views
1

美好的一天,Stackoverflow!如何使这与Windows兼容?

我将我的一个Linux脚本移植到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 

服务器套接字之间的整个3方式交替,所述脚本和子过程的输出信道(以及输入通道的标准输入,因为我的脚本将写入该脚本,但该脚本不在select()列表中)是脚本中最重要的部分。

我知道,对于Windows,win32api模块中有win32pipe。问题是,找到这个API的资源非常困难,而我发现的并不是很有帮助。

如何利用此win32pipe模块来做我想做的事?我有一些消息来源的地方是在一个不同但类似的情况被使用,但弄得我非常:

if os.name == 'nt': 
    import win32pipe 
    (stdin, stdout) = win32pipe.popen4(" ".join(server_args)) 
else: 
    server = Popen(server_args, 
    stdout = PIPE, 
    stdin = PIPE, 
    stderr = PIPE) 
    outputs = [server.stderr, server.stdout, sys.stdin] 
    stdin = server.stdin 

[...] 

while True: 
    try: 
     if os.name == 'nt': 
     outready = [stdout] 
     else: 
     outready, inready, exceptready = select.select(outputs, [], [], 1.0) 
    except: 
     break 

stdout这里是已经开始与win32pipe.popen4(...)

子进程的组合输出和错误

提出的问题有:

  • 为什么不是select()为windows版本?这不行吗?
  • 如果不使用select()那里,我怎么能实现neccessary超时是select()提供(这显然不会像这样在这里工作)

请帮助我!

回答

0

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