wxGrid crashes when updated too fast.

Hi.
I would very much appreciate your help regarding the following issue.

I'm building an application that among other things displays live prices for
a number of financial instruments in a Grid.
I noticed that the application crashes when the price changes are frequent (
I call SetCellValue(...) repeatedly with
a small very small interval in between). I wrote a small program that
reproduces the behavior which I'm ataching below.
This is my first wxPython program, so I'm thinking I might be diong something
wrong.

I'm using Python 2.3, wxPython 2.5.2.8 running on Windows (NT4.0 and XP
Proffesional).

···

#
-----------------------------------------------------------------------------------------------
import wx, threading, Queue, wx.grid as gridlib, time

# how fast the producer thread enques items
DT_PRODUCER = 0.05
# how often the GUI checks the queue for new items to be displayed.
DT_CONSUMER = 1
# how many cols for the grid.
NUM_COLS = 10

MAX_NUM_PROCESSED = 1000

# used to pass items from the producer thread to the GUI.
queue = Queue.Queue()

def data_source():
    """
    Runs in a separate thread than the GUI.
    Produces items and places them in the queue to be displayed by the GUI.
    """

    # the two items we're producing over and over again.
    item1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    item2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

    flag = True
    while True:
        time.sleep(DT_PRODUCER)
        if flag: queue.put(item1)
        else: queue.put(item2)
        flag = not flag

#
------------------------------------------------------------------------------------------------------------------

class FrameWithGrid(wx.MDIChildFrame):
    " MDI Child frame containing a Grid. This is where the data is actaully
displayed."

    def __init__(self, parent, id, title):

        wx.MDIChildFrame.__init__(self, parent, id, title)
        self.panel = wx.Panel(self, -1)

        # create the grid.
        self.grid = gridlib.Grid(self.panel, -1)
        self.grid.CreateGrid(1, NUM_COLS, 0)

        # formatting stuff.
        self.grid.SetRowLabelSize(0)
        self.grid.SetColLabelSize(15)
        for col in range(NUM_COLS): self.grid.SetColSize(col, 70)
        self.grid.SetSize((700, 75))
        self.SetSize((710, 100))

#
------------------------------------------------------------------------------------------------------------------

class GuiPart(wx.MDIParentFrame):
    " This is the main frame of the application."

    def __init__(self, title):
        wx.MDIParentFrame.__init__(self, None, -1, title, size=(750, 300))
        self.child_frame = FrameWithGrid(self, -1, "MyGrid")

        # schedule looking for items in the queue.
        self.schedule_check()

    def check_queue(self):
        " Display items found in queue"

        count = 1
        while count < MAX_NUM_PROCESSED:
            try:
                item = queue.get(0)
                for col in range(10):
                    self.child_frame.grid.SetCellValue(0, col, str(item[col]))
            except Queue.Empty:
                break
            count += 1

        # re-schedule iteself.
        self.schedule_check()

    def schedule_check(self):
        " Schedule checking the queue."
        t = threading.Timer(DT_CONSUMER, self.check_queue)
        t.start()
        
#
---------------------------------------------------------------------------------------------------------------------

class App(wx.App):
    def OnInit(self):
        gui = GuiPart("MyFrame")
        gui.Show(True)
        self.SetTopWindow(gui)
        return True
    
app = App(0)
thread = threading.Thread(target = data_source)
thread.start()
app.MainLoop()

Please follow the attached hyperlink to an important disclosure:
<http://www.csfb.com/legal_terms/market_commentary_disclaimer.shtml>.

==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.

Ciordas, Calin wrote:

Hi.
I would very much appreciate your help regarding the following issue.

I'm building an application that among other things displays live prices for
a number of financial instruments in a Grid.
I noticed that the application crashes when the price changes are frequent (
I call SetCellValue(...) repeatedly with
a small very small interval in between). I wrote a small program that
reproduces the behavior which I'm ataching below. This is my first wxPython program, so I'm thinking I might be diong something
wrong.

I'm using Python 2.3, wxPython 2.5.2.8 running on Windows (NT4.0 and XP
Proffesional).

Don't call SetCellValue from the thread. Instead you can use wx.CallAfter to schedule the function call to happen in the main GUI thread.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!