2

Trying to run a time consuming task from a wxpython GUI. The basic idea is to start the long time task from the GUI (pressing a button) and then, a static text on the dialog should be updated from it.

First I tried some threading (http://wiki.wxpython.org/LongRunningTasks and many other resourses seen), and I want to show back the messages using Publisher.class. It didn't went so well, after a message or two, the GUI seems to frozen.

Now I want to achieve that with multiprocessing. I have this method inside my 'GUI' class:

        def do_update(self, e):
            self.txt_updatemsg.SetLabel("Don't stop this \n")
            ...

            pub = Publisher() # i tried also calling directly from dbob object
            # Publisher() is a singleton so this must be useless?
            pub.subscribe(self.__update_txt_message, ('updatedlg', 'message'))

            dbob = dbutils.DBUtils() # DBUtils is the class with 'long time' tasks
            dbob.publisher = pub

            p = Process(target=self.do_update_process, args=(dbob,))
            p.start()
            while p.is_alive:
                wx.Yield

        def do_update_process(self, dbob):
            dbob.do_update()

__update_txt_message is a simple function what sets the static text on dialog.

Question is: how can I send back some text messages from this Process (just simple text, that's all I need) Thanks guys!

wxpydon
  • 61
  • 1
  • 6
  • After some more work, I end up with the right message in __update_txt_message method (connected to Publisher class). If I put print message.data it works, I can see it in the console. But it doesn't update the Static Text. So now the question is: how can I force the update? Still digging... – wxpydon May 18 '10 at 14:48
  • Why did you give up on using threads? Generally, the best solution is to use a thread for the long running process and use queues to communicate between the main thread and the long running process thread. – new name May 19 '10 at 03:19
  • Well, I don't think the problem now is using threads or Processes. I am able to get the needed messages in __update_txt_message but from there I cannot change the static labels, which seems totally weird. I can print the message to console, I can SetLabel and then GetLabel to see, ok the msg is there, but not on the displayed Dialog! – wxpydon May 19 '10 at 05:54

3 Answers3

3

Robin Dunn kindly answered me in wxpython mailing list

The pubsub module is not able to cross process boundaries. You'll need to use the classes provided by the multiprocessing module, or some other inter-process communication method, to communicate between the parent and child processes.

So I fixed my issue using threading module.

wxpydon
  • 61
  • 1
  • 6
1

The standard way is to use one or more Queue objects to pass data back and forth to a multithreading.Process. It works more or less the same way with threading.

Here is an example: http://wiki.wxpython.org/MultiProcessing

user1594322
  • 2,008
  • 3
  • 19
  • 16
0

wx.CallAfter(function)

Steven Sproat
  • 4,398
  • 4
  • 27
  • 40
  • Where this should be placed to? Inside do_update() method of the dbob object? What about function arg? – wxpydon May 18 '10 at 13:34
  • 2
    As far as I can see, CallAfter will help me run a function after the process work, but what I want is to get some text messages during this work. – wxpydon May 18 '10 at 14:49