允许多个Python GUI同时运行相同的exe

时间:2014-02-28 18:47:33

标签: windows multithreading python-2.7 user-interface

我开发的Python GUI在同一目录中执行exe文件。我需要允许用户打开GUI的多个实例。这会导致同时调用相同的exe并引发以下错误:the process can not access the file because it is being used by another process。我在python GUI中使用专用线程来运行exe。

如何让多个GUI同时运行同一个exe?

我很感激代码示例。

以下是主题。运行包括执行exe。这个exe是使用fortran制作的。

class LineariseThread(threading.Thread):
def __init__(self, parent):
    threading.Thread.__init__(self)
    self._parent = parent


def run(self):

    self.p = subprocess.Popen([exe_linearise], shell=True, stdout=subprocess.PIPE)

    print threading.current_thread()

    print "Subprocess started"
    while True:
        line = self.p.stdout.readline()
        if not line:
            break      
        print line.strip()
        self._parent.status.SetStatusText(line.strip())
        # Publisher().sendMessage(('change_statusbar'), line.strip())
        sys.stdout.flush()

    if not self.p.poll():
        print " process done"
        evt_show = LineariseEvent(tgssr_show, -1)
        wx.PostEvent(self._parent, evt_show)


def killtree(self, pid):  
    print pid
    parent = psutil.Process(pid)
    print "in killtree sub: "
    for child in parent.get_children(recursive=True):
        child.kill()
    parent.kill()



def abort(self):
    if self.isAlive():
        print "Linearisation thread is alive"

        # kill the respective subprocesses
        if not self.p.poll():
            # stop them all
            self.killtree(int(self.p.pid))


        self._Thread__stop()
        print str(self.getName()) + " could not be terminated"

        self._parent.LineariseThread_killed=True

1 个答案:

答案 0 :(得分:0)

我想我想出了一种避免错误的方法。实际上并不是exe的执行引发了错误。当exe访问由同一个exe的另一个实例锁定的其他文件时引发的错误。因此,我决定不允许运行多个exe实例。相反,我想到允许在单个实例中打开多个案例。这样我就可以管理进程线程以避免上述问题。

我应该提一下,给我的评论帮助我详细研究错误信息,以弄清楚究竟发生了什么。