运行长Popen命令时无法退出

时间:2014-02-10 21:58:41

标签: python popen quit

我正在运行以下代码,该代码运行一个运行约40分钟的命令行应用程序。在运行时,我的QUIT按钮无法访问,因此我无法退出正在运行的应用程序。以下代码和按钮都位于自己的def中。关于在我的应用程序运行时如何获得工作退出按钮的任何想法?

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

1 个答案:

答案 0 :(得分: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)