[wxPython-users] Problem in threading

Josiah Carlson jcarlson at uci.edu
Thu Feb 2 11:00:48 EST 2006


My response is at the end.

Sbaush <sbaush at gmail.com> wrote:
> Hi all.
> I've a problem with thread in python.
> My applications has a GUI that has some button.
> I've a MVC-like architecture.
> If i click on a button there is this effect:
> 
> view.button_1_delete.Bind(EVT_BUTTON,self.OnDelete)
> view.button_1_list.Bind(EVT_BUTTON,self.OnList)
> 
> def OnDelete(self, evt):
>         self.presenter.updateModel("delete")
> def OnList(self, evt):
>         self.presenter.updateModel("list")
> 
> Probably you image that the boss of this program is updateModel function.
> this function has this code:
> 
> def updateModel(self,Type):
>         iptType=Type
>         self.readGUI(iptType)  # This function takes the request info from
> GUI
>         packet=self.ipt_writeXML(iptType) # Put info in XML string
>         self.sender.send(packet) #Send the XML string
>         (xmlresponse,hostfrom)=self.receiver.receive() # Receive a XML
> string response from agent and the IP of agent
>         self.iptResponse.run(xmlresponse,hostfrom) # view response in a
> separate view.
> 
> Now it works great, but there is a big problem: my app wait for response
> before allow user to do another request.
> I thought that with threads it could works perfectly.
> I tried to apply threads but without any good result.
> Have you idea how can i do it and how can i apply thread to it?

import wx
import wx.lib.newevent
import threading

ResponseEvent, EVT_RESPONSE = wx.lib.newevent.NewEvent()

...
    def __init__(self, ...):
        ...
        self.Bind(EVT_RESPONSE, self.GotResponse, self)
        ...

    def _updateModel(self, Type):
        iptType=Type
        self.readGUI(iptType)
        packet=self.ipt_writeXML(iptType)
        self.sender.send(packet)
        (xmlresponse,hostfrom)=self.receiver.receive()
        wx.PostEvent(self,
             ResponseEvent(xml=xmlresponse,
                           host=hostfrom))

    def updateModel(self, Type):
        threading.Thread(target=self._updateModel,
                          args=(Type,)).start()

    def GotResponse(self, evt):
        self.iptResponse.run(evt.xml, xml.host)
    ...


 - Josiah




More information about the Python-list mailing list