Threading confusion

How can I run another non-gui piece of code asynchronously?

As far as GUI components, any wxpython classes that participate in the event loop appear to behave in a multi-threaded manner. I don’t have to explicitly manage threading. I would like to have a toggle button that runs another non-gui piece of code asynchronously that will be interrupted when the button becomes un-toggled. This other code is currently in a separate class. Is there some generalized wx object I can inherit from to get this functionality, or do I have create threads manually?

If I understand you correctly, you want to control the execution of some
thread X with some toggle (button, checkbox, etc.) in the GUI. Here is
some code that should get you started.

- Josiah

import wx
import threading

QUIT, WAIT, RUN = wx.NewId(), wx.NewId(), wx.NewId()

TODO = WAIT

def external_thread():
    while TODO != QUIT:
        if TODO == WAIT:
            time.sleep(.1)
        elif TODO == RUN:
            #do whatever repeated operation you want

def start_external_thread():
    threading.Thread(target=external_thread).start()

class TogglePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        
        rb1 = wx.RadioButton(self, QUIT, "Quit", style=wx.RB_GROUP)
        rb2 = wx.RadioButton(self, WAIT, "Wait")
        rb3 = wx.RadioButton(self, RUN, "Run")
        
        rb2.SetValue(1)
        
        self.Bind(wx.EVT_RADIOBUTTON, self.OnRadio)

        sz = wx.BoxSizer(wx.VERTICAL)
        sz.Add(rb1)
        sz.Add(rb2, 0, wx.TOP, 5)
        sz.Add(rb3, 0, wx.TOP, 5)
        self.SetSizer(sz)
    
    def OnRadio(self, evt):
        global TODO
        TODO = evt.GetEvtObject().GetId()

···

"jeff sacksteder" <jsacksteder@gmail.com> wrote:

How can I run another non-gui piece of code asynchronously?

As far as GUI components, any wxpython classes that participate in the event
loop appear to behave in a multi-threaded manner. I don't have to explicitly
manage threading. I would like to have a toggle button that runs another
non-gui piece of code asynchronously that will be interrupted when the
button becomes un-toggled. This other code is currently in a separate
class. Is there some generalized wx object I can inherit from to get this
functionality, or do I have create threads manually?

I found a bit more documentation that I had overlooked- it seems that I need
to somehow listen for wx events in my non-gui class to detect togglebutton
state changes. The example in the example is the inverse of what I want. Do
do I go about listening to the wx event loop?

Should I just make my class an invisible frame?

jeff sacksteder wrote:

How can I run another non-gui piece of code asynchronously?

As far as GUI components, any wxpython classes that participate in the event loop appear to behave in a multi-threaded manner.

Not really. It just dispatches to event handlers and other callbacks from a central loop. While any of them are running none of the others (or the main event loop) are able to run, and only one event handler can be running at a time.

I don't have to explicitly manage threading. I would like to have a toggle button that runs another non-gui piece of code asynchronously that will be interrupted when the button becomes un-toggled. This other code is currently in a separate class. Is there some generalized wx object I can inherit from to get this functionality, or do I have create threads manually?

See the thread sample in the demo, and also http://wiki.wxpython.org/index.cgi/LongRunningTasks for ideas.

···

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