Changeset 428


Ignore:
Timestamp:
02.08.2011 19:00:13 (8 years ago)
Author:
marcus
Message:

Resolved issue between pubsub and anythread.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • SHX/trunk/sandbox/pubsubthread.py

    r427 r428  
    11# -*- coding: utf-8 -*- 
     2 
     3""" 
     4Demo of multi-threaded messaging system and wx. 
     5""" 
    26 
    37import threading 
     
    913 
    1014class thready(threading.Thread): 
     15    """ 
     16    So-called worker thread doing something. Sends randomly messages including 
     17    it's signature. This thread knows nothing about the possible listeners! 
     18    """ 
    1119    def __init__(self, name, main): 
    1220        threading.Thread.__init__(self) 
     
    3038            # random update of main threads status bar 
    3139            if randint(0, 20) == 10: 
    32 #                msgs.sendMessage("ui.change.statustext", text=self.name) 
    33                 self.main.UpdateStatusText(text=self.name) 
     40                msgs.sendMessage("ui.change.statustext", text=self.name) 
     41                msgs.sendMessage(self.name, content="self called") 
    3442 
    3543            time.sleep(.1) 
     
    3745    def monitor(self, content): 
    3846        print time.strftime("%H:%M:%S", time.localtime()), self.name, \ 
    39                                                             "received", content 
    40         if not self.name.endswith("zwo"): 
    41             msgs.sendMessage("thread.zwo", content="test") 
     47                                                           "received:", content 
     48        if not self.name.endswith("two"): 
     49            msgs.sendMessage("thread.two", content="test from "+self.name) 
    4250 
    4351    def stop(self): 
     
    5462        self.Bind(wx.EVT_CLOSE, self.OnQuit) 
    5563 
    56         tlist = ['eins', 'zwo'] 
     64        tlist = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'] 
    5765 
    5866        for i, name in enumerate(tlist): 
     
    6270        self.tlist = tlist 
    6371 
    64         msgs.subscribe(self.UpdateStatusText, "ui.change.statustext") 
    65 #        print msgs.exportTopicTree() 
     72        msgs.subscribe(self.ListenUpdateStatusText, "ui.change.statustext") 
    6673 
    6774        self.Show() 
     
    7481        evt.Skip() 
    7582 
     83    def ListenUpdateStatusText(self, text): 
     84        """ 
     85        This method provides the correct interface for pubsub. 
     86        """ 
     87        self._UpdateStatusText(text=text) 
     88 
    7689    @anythread 
    77     def UpdateStatusText(self, **kwargs): 
     90    def _UpdateStatusText(self, **kwargs): 
     91        """ 
     92        Since the anythread decorator alters the method's interface, using 
     93        pubsub will because of it's rigid argument testing. 
     94        """ 
    7895        self.SetStatusText(kwargs.get("text", "")) 
    7996 
     
    85102 
    86103if __name__ == "__main__": 
    87 # 
    88 #    time.sleep(1) 
    89 #    msgs.sendMessage("thread.eins", content="nummer 1") 
    90 #    msgs.sendMessage("thread.zwo", content="nummer 2") 
    91 #    time.sleep(1) 
    92 # 
    93 # 
    94 #    print "-> end" 
    95  
    96104    app = wx.App(redirect=False) 
    97105    frm = MinimalApp(title="wx pubsub threading test", size=(400,50)) 
Note: See TracChangeset for help on using the changeset viewer.