2014-02-10 71 views
0

我正在運行下面的代碼,該代碼運行運行約40分鐘的命令行應用程序。在運行時,我的QUIT按鈕不可訪問,所以我無法退出正在運行的應用程序。下面的代碼和按鈕都在他們自己的def中。我的應用程序運行時如何獲得工作退出按鈕的任何想法?長時間運行Popen命令時無法退出

command1 = transporterLink + " -m verify -f " + indir1 + " -u " + username + " -p " + password + " -o " + logPath + " -s " + provider1 + " -v eXtreme" 
master, slave = pty.openpty() 

process = Popen(command1, shell=True, stdin=PIPE, stdout=slave, stderr=slave, close_fds=True) 
stdout = os.fdopen(master) 
global subject 
subject = "Test" 
while True: 
    wx.Yield() 
    line = stdout.readline() 
    line = line.rstrip() 
    print line 
    if "Returning 1" in line: 
     result1 = "Verify FAILED!" 
     subject = "FAILED! - " 
     self.sendEmail(self) 
     break 
    if "Returning 0" in line: 
     result1 = "Verify PASSED!" 
     subject = "PASSED! - " 
     self.sendEmail(self) 
     break 
+0

請解釋'wx.Yield()'。這是一個GUI應用程序嗎?使用哪個GUI框架? (我們可以猜出,但不應該)。它是如何稱呼上面顯示的代碼的? –

+0

對不起,是的,使用wxpython。 – speedyrazor

回答

0

stdout.readline阻塞,直到有東西在stdout。您可以使用select module's poll

command1 = transporterLink + " -m verify -f " + indir1 + " -u " + username + " -p " + password + " -o " + logPath + " -s " + provider1 + " -v eXtreme" 
master, slave = pty.openpty() 

process = Popen(command1, shell=True, stdin=PIPE, stdout=master, stderr=slave, close_fds=True) 
stdout = os.fdopen(master) 
import select 
q = select.poll() 
q.register(stdout,select.POLLIN) 
global subject 
subject = "Test" 
while True: 
    wx.Yield() 
    events = q.poll(0) 
    for fd, flags in events: 
     assert(fd == stdout.fileno()) 
     line = stdout.readline() 
     print line 
     if "Returning 1" in line: 
      result1 = "Verify FAILED!" 
      subject = "FAILED! - " 
      self.sendEmail(self) 
      sys.exit(0) 
     if "Returning 0" in line: 
      result1 = "Verify PASSED!" 
      subject = "PASSED! - " 
      self.sendEmail(self) 
      sys.exit(0) 
+0

乾杯雷姆斯,與您的代碼我越來越line = line.rstrip() AttributeError:'列表'對象沒有屬性'rstrip'。如果我把這條線路打印出來並打印出來,我會得到[(6,1)]的無盡列表。我可以退出使用按鈕,這是好的,但我現在沒有得到打印輸出。 – speedyrazor

+0

根據http://docs.python.org/2/library/select.html#select.poll.poll更新了代碼 – Rems

相關問題