Subscribe problem (not sticking)

29 views
Skip to first unread message

Eric

unread,
Nov 5, 2012, 1:53:20 AM11/5/12
to pypu...@googlegroups.com

Hi all,
Having trouble getting a "subscribe" to take.  This is a wxPython app but I am using pubsub directly.  I'm using Wing on Win 7 with Python 2.7.3.

I have a main module that sets up the app.MainLoop, a module that has all the wxPython code (actually two, I'm using wxFormBuilder so there is their output module and then mine for all my event code, etc.), a module with the topic tree classes and another module for other processing tasks.

If I set a breakpoint on "pub.subscribe(self.search, "agile.search")" in my processing module, the subscription works, self.search gets the message.  If I don't set a breakpoint, self.search doesn't get the message?  I setup a listener on pub.ALL_TOPICS and the message is always going out.  If I put "printTreeDocs(extra='L')" right after the subscribe, the printed tree shows the listener, but the one right before going into app.MainLoop() doesn't show it.

Weak reference problem?  Looks like the listener goes away for some reason.  Guessing this is a basic Python problem that I'm missing...

The topic tree prints fine so I'm skipping showing it.

Thanks for the help,
Eric

========================================================================
Main module:
========================================================================
import wx
from pubsub import pub
from pubsub.utils.topictreeprinter import printTreeDocs

import uiclass

import pubsubstuff
import processing

########################################################################
class Main(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Display the GUI"""

        proc = processing.Processing()
        uiclass.UIclass(None).Show()


if __name__ == "__main__":
    app = wx.App(False)
    pub.importTopicTree(pubsubstuff)
    pub.exportTopicTree('kwargs_topics_out')
    pub.setTopicUnspecifiedFatal()
    Main()
    printTreeDocs(extra='L')  # the search listener isn't shown
    app.MainLoop()
   
========================================================================
uiclass ("wrapper for the wxFormBuilder output")
========================================================================
from pubsub import pub

# wxFormBuilder output
import wxAgileHistory

# Create main GUI
class UIclass(wxAgileHistory.fraMain):
    def __init__(self, parent):

        wxAgileHistory.fraMain.__init__(self, parent)
        pub.subscribe(self.init_app, "app.init")  # this always works
   
========================================================================
processing (misc functions, the app interacts with a webpage
========================================================================
from pubsub import pub
from pubsub.utils.topictreeprinter import printTreeDocs

########################################################################
class Processing(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""

        pub.subscribe(self.search, "agile.search")
        printTreeDocs(extra='L')  # this shows the listener

    #----------------------------------------------------------------------
    def search(self, change):
        """"""
        print "Hit search"

oliver

unread,
Nov 5, 2012, 7:31:20 AM11/5/12
to PyPubSub
Hi Eric, you are right that the issue is a weak reference: Main is a class, so your statement 

Main() 

does not call a function, it creates an instance of Main and immediately discards it. Looks like similar issue with Processing: 

proc = Processing()

creates a local in __init__ instead of a data member, so when __init__ finishes, proc is discarded. Try instead

main = Main()

to create a local strong ref, and 

self.proc = Processing() 

so the proc will be alive for the duration of the app loop. 

Oliver


--
You received this message because you are subscribed to the Google Groups "pypubsub" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pypubsub/-/9Qpc-0Fui5IJ.
To post to this group, send email to pypu...@googlegroups.com.
To unsubscribe from this group, send email to pypubsub+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pypubsub?hl=en.

Eric

unread,
Nov 6, 2012, 12:59:12 AM11/6/12
to pypu...@googlegroups.com
Hi Oliver,
Saved again!  I'm a hardware engineering guy and self educated in Python, there are obviously have some gaps in my education...  I went and read up on object lifetime so hopefully I won't do this again soon.

Thanks,
Eric
Reply all
Reply to author
Forward
0 new messages