创建 wx.App 的多个实例 - 可以吗?

Creating multiple instances of wx.App - is it ok?

所以我需要实现以下场景: - 多个任务作为进程同时 运行ning。 - 每个任务都应该显示一个带有 "Cancel" 按钮的进度条,点击它应该终止它。

为了实现响应式 GUI,我 运行 每个进程的任务都在一个单独的线程中,看来我还需要为每个进程创建一个单独的 wx.App ,否则线程好像不是运行ning。此设置工作正常,但是:

a) 我不确定多个 wx.App 是个好主意还是

b) 如果有更好的方法来实现我的目标。

下面的

MWE(注意:在这个示例代码中我可以使用wx.ProgressDialogUpdate方法来识别"Cancel"按钮是否被按下,但不能这样做对于我的实际应用程序)。

import wx, multiprocessing, time, psutil
from multiprocessing import Queue
from threading import Thread
from wx.lib.pubsub import pub as Publisher

#runs the task
def task_runner(q):
    pid = multiprocessing.current_process().pid
    q.put(pid)

    while True:
        print("Process Running")
        time.sleep(1)
        wx.CallAfter(Publisher.sendMessage, "update") #call to update the bar

class TestPanel():

    def __init__(self,name):
        self.q = Queue()
        self.count=0
        max = 80

        # dialog to see progress and cancel the task
        self.dlg = wx.GenericProgressDialog(name,
                               "An informative message",
                               maximum = max,
                               parent=None,
                               style = wx.PD_CAN_ABORT
                                | wx.PD_APP_MODAL
                                | wx.PD_ELAPSED_TIME
                                )

        #set listener to dialog's "Cancel" button
        for child in self.dlg.GetChildren():
            if isinstance(child, wx.Button):
                cancel_function = lambda evt, parent=self.dlg: self.onClose(evt, parent)
                child.Bind(wx.EVT_BUTTON, cancel_function)

        #subscribe to update the progress bar from the thread
        Publisher.subscribe(self.updateProgress, "update")


        # start thread which runs some task
        p = Thread(target=task_runner, args=(self.q,))
        p.start()


    #updates the progress bar
    def updateProgress(self):
        print("updating progress")
        self.count=self.count+10
        self.dlg.Update(self.count)

    #kills the process
    def kill(self, proc_pid):
            process = psutil.Process(proc_pid)
            for proc in process.children(recursive=True):
                proc.kill()
            process.kill()

    #closing the dialog event
    def onClose(self, event, dialog):
        """"""
        print "Closing dialog!"
        pid = self.q.get()
        self.kill(pid)
        dialog.Destroy()

# run process, each process creates its own wx.App
def runProcess(name):
    app = wx.App(False)
    TestPanel(name)
    app.MainLoop()


# worker class to use for multiprocessing pool
class Worker():
    def __call__(self, name):
        return runProcess(name)


if __name__ == '__main__':
    items=['Bar1', 'Bar2']
    pool = multiprocessing.Pool(processes=2)
    result = pool.map(Worker(), items) #create two processes
    pool.close()

不,在一个进程中有多个 wx.App 不是一个好主意。即使在先验完成后创建一个新的有时也会有问题。

但是,由于您使用的是 multiprocess,因此并不完全相同。除非我遗漏了什么,否则每个 OS 进程在你的情况下确实只有一个 wx.App,并且由于父进程没有创建 wx.App 那么他们不会试图继承那个(这可能会导致更多问题。)