0

I have any issue with wxpython's textctrl and threading. Would appreciate any help in resolving this issue.

My program processes files, as and when each file is processed it is listed within the textctrl as being completed. When working with just a few files the textctrl is responsive and displays itself immediately and does not disappear. Even if these files are large. Did a test on a 700mb file and textctrl worked perfectly.

The problem occurs when workin on many files, say 20+ for exmaple. Under these circumstances the textctrl disappears for 6 or 7 seconds, then reappears and works as normal.

I have tried normal threading, daemon threading etc.. Also tried using .join() which made things even worse. I'm wondering if this is just because my program is very processor intensive, or if I'm just doing something wrong.

My thread line of code is listed below. So far this is by far the fastest method, just not good enough for my purposes. Thanks in advance, Clinton.

def Worker(self, e, _file):

    match = ''

    with open(_file, 'r') as f:
        data = f.read()

    for char in data:
        if char in self.key:
            match += chr(self.key.index(char))

    open(_file, 'w').close()

    with open(_file, 'w') as f:
        f.write(match)

    wx.CallAfter(self.ListFilesEncrypt, e, _file)

if __name__ == '__main__':
    for _file in self.file2process:
        self.filenum += 1
        Thread(target=self.Worker, args=(e, _file,)).start()
Clinton Moffat
  • 211
  • 1
  • 2
  • 12
  • How are you updating the text control from the thread? If you are calling the text control directly, then you have a problem – Mike Driscoll Aug 28 '13 at 17:57
  • Are you saying you are creating multiple threads that can update the text control, possibly at once? – Mike Driscoll Aug 28 '13 at 19:14
  • To be honest not sure as really new to python and programming. The code above works but when a lot of files are selected to be processed the textctrl disappears for 6 seconds then reappears. Once it reappears it works as it should do. As each file has been processed it gets printed into the textctrl. So the textctrl output is similar to that of the linux ls command. Once printing start it works fine, its just that intial annoying delay. – Clinton Moffat Aug 28 '13 at 19:20
  • I updated my answer with a code example – Mike Driscoll Aug 28 '13 at 19:31

4 Answers4

3

Update the GUI using thread-safe methods. In wxPython, there are 3:

  • wx.CallAfter
  • wx.CallLater
  • wx.PostEvent

You should also take a look the wxPython wiki for information on wxPython and threading:

I also wrote a tutorial on the topic:

UPDATE: Here is a simple example that creates 40 threads and "processes" 40 made up files. It updates the display when each thread is done. However, I do not see the issue you speak of.

import random
import time
import wx

from threading import Thread
from wx.lib.pubsub import Publisher

########################################################################
class TestThread(Thread):
    """Test Worker Thread Class."""

    #----------------------------------------------------------------------
    def __init__(self, fname, sleepAmt):
        """Init Worker Thread Class."""
        Thread.__init__(self)
        self.fname = fname
        self.sleepAmt = sleepAmt
        self.start()    # start the thread

    #----------------------------------------------------------------------
    def run(self):
        """Run Worker Thread."""
        # This is the code executing in the new thread.
        time.sleep(self.sleepAmt)
        msg = "%s finished in %s seconds!" % (self.fname, self.sleepAmt)
        wx.CallAfter(Publisher().sendMessage, "update", msg)


########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        panel = wx.Panel(self, wx.ID_ANY)
        self.updateText = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        self.btn = btn = wx.Button(panel, label="Start Thread")

        btn.Bind(wx.EVT_BUTTON, self.onButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.updateText, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

        # create a pubsub receiver
        Publisher().subscribe(self.updateDisplay, "update")

    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        Runs the thread
        """
        for i in range(40):
            fname = "test%s.txt" % i
            secs = random.choice(range(3, 15))
            TestThread(fname, secs)

    #----------------------------------------------------------------------
    def updateDisplay(self, msg):
        """
        Receives data from thread and updates the display
        """
        data = msg.data + "\n"
        self.updateText.WriteText(data)

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

I am running on Windows 7 using Python 2.6 with wxPython 2.8.12.1

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Thanks for reply. Should have mentioned I am already using ww.CallAfter. I have even tried removing it all together just to see what effect it has. Answer none what so ever, even when not printing to textctrl. It has something to do with the number of file selected for processing. Clinton. – Clinton Moffat Aug 28 '13 at 18:17
  • I have edited question and have provided code for the worker thread function. – Clinton Moffat Aug 28 '13 at 18:28
  • Thanks Mike for the code example. Reason you do not get same problem as I do may well come down to the fact that your code is some what different to mine. I am still currently working on my first problem, currently around 300 or so line of code, so no Hello World for me :-) I tlooks like however that I am going to have to re-write again ( 5 or 6 times now ) using your code as an example. It amazes me just how difficult it is to wite a GUI programm in comparison to a command line program. For me it has double the amount of code and trebled the complexity. Thanks again, Clinton. – Clinton Moffat Aug 28 '13 at 20:43
  • 1
    Yeah, GUIs take a while. But with practice, you get a lot better and a lot faster. – Mike Driscoll Aug 28 '13 at 20:46
0

Your threads should not update the text control directly - they should either use wx.CallAfter or, better, set a flag for the main, (GIU), thread to update the control - it sounds like a on end thread method might be appropriate.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • Thanks for reply. As per my reply to Mike Driscoll's response I am already using wx.CallAfter so that is not the problem. It's more to do with the number of files that have been selected for processing. Even removed wx.callafter so there was no printing to textctrl. Still got same problem. I will look into your other 2 suggestions though. Clinton. – Clinton Moffat Aug 28 '13 at 18:22
0

More than CPU-intensive, it can be IO-intensive. And IO-intensive applications can have a very big impact on performance, especially when you are also using the IO-units for other critical purposes like paging. I'd suggest to serve 5 to 10 at a time and queue the rest.

Mario Rossi
  • 7,651
  • 27
  • 37
  • Thanks for the reply. Did try queue once before without success, but will try again. I'm still very new to programming so may of messed that up in some way. – Clinton Moffat Aug 28 '13 at 18:24
0

I am using wxpython 4.1.1 and have found that this no longer works.

wxPyDeprecationWarning: wx.lib.pubsub has been deprecated, plese migrate your code to use pypubsub, available on PyPI.
======================================================================
          ***    ATTENTION   ***
This messaging protocol is deprecated. This module, and hence arg1
messaging protocol, will be removed in v3.4 of PyPubSub. Please make
the necessary changes to your code so that it no longer requires this
module. The pypubsub documentation provides steps that may be useful
to minimize the chance of introducing bugs in your application.
======================================================================