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

有些话、适合烂在心里 提交于 2021-02-11 06:18:26

问题


so I need to implement a following scenario: - Several tasks are running simultaneously as processes. - Each task should display a progress bar with a "Cancel" button, clicking on which should terminate it.

To achieve responsive GUI, I run the task for each process in a separate thread, and it seems that I need to create a separate wx.App for each process as well, otherwise the thread seems to be not running. This setup works fine, however:

a) I am not sure whether multiple wx.App's is a good idea or

b) If there is a better way of achieving my goal.

MWE below (note: in this sample code I could have used Update method of wx.ProgressDialog to identify whether the "Cancel" button has been pressed, but can't do so for my real application).

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()

回答1:


No, having more than one wx.App in a single process is not a good idea. Even creating a new one after the prior is finished can sometimes be problematic.

However since you are using multiprocess it is not quite the same. Unless I'm missing something, each OS process does have just one wx.App in your case, and since the parent process is not creating a wx.App then they are not trying to inherit that one (which would likely cause even more problems.)



来源:https://stackoverflow.com/questions/41154808/creating-multiple-instances-of-wx-app-is-it-ok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!