Erstellen mehrerer Instanzen von WX.App - Ist es in Ordnung?Python

Python-Programme
Anonymous
 Erstellen mehrerer Instanzen von WX.App - Ist es in Ordnung?

Post by Anonymous »

Ich muss also ein folgendes Szenario implementieren:
- Mehrere Aufgaben werden gleichzeitig als Prozesse ausgeführt. < /p>

Um eine reaktionsschnelle GUI zu erreichen, führe ich die Aufgabe für jeden Prozess in einem separaten Thread aus, und es scheint, dass ich auch für jeden Prozess ein separates WX.App < /code> erstellen muss, ansonsten scheint der Thread nicht ausgeführt zu werden. Dieses Setup funktioniert jedoch einwandfrei: < /p>

A) Ich bin nicht sicher, ob mehrere WX.App < /code> eine gute Idee oder < /p>

b ist, wenn es eine bessere Möglichkeit gibt, mein Ziel zu erreichen. WX.ProgressDialog < /code>, um festzustellen, ob die Taste "Abbrechen" gedrückt wurde, kann dies jedoch für meine reale Anwendung nicht tun). < /p>

Code: Select all

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

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post