创建 wx.App 的多个实例 - 可以吗? [英] Creating multiple instances of wx.App - is it ok?

查看:71
本文介绍了创建 wx.App 的多个实例 - 可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

为了实现响应式GUI,我将每个进程的任务运行在一个单独的线程中,似乎我还需要为每个进程创建一个单独的wx.App,否则线程似乎不跑步.但是,此设置运行良好:

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) 我不确定多个 wx.App 是个好主意还是

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

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

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

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

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

推荐答案

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

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.

然而,由于您使用的是 multiprocess,所以它并不完全相同.除非我遗漏了什么,否则每个操作系统进程在你的情况下只有一个 wx.App,并且由于父进程没有创建一个 wx.App 那么它们是不要试图继承那个(这可能会导致更多问题.)

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

这篇关于创建 wx.App 的多个实例 - 可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆