Publisher()在Thread中不起作用

时间:2013-12-27 12:40:30

标签: python multithreading

我在MVC模式中重写我的程序,一切都很好,但现在不行。发布者不会向订阅者发送消息。

from wx.lib.pubsub import pub
from threading import Thread

Publisher = pub.Publisher()

class GameDataLoading(object):
    def __init__(self):
        self.view = DataLoadingView()
        self.view.Center()
        self.view.Show()

        Publisher().subscribe(self.test, 'test')
        LoadGameData().start()

    def test(self, data):
        print 'working'

class LoadGameData(Thread):
    def __init__(self):
        super(LoadGameData, self).__init__()

    def run(self):
        wx.CallAfter(Publisher().sendMessage, 'test')

此代码有什么问题?

1 个答案:

答案 0 :(得分:0)

代码是否保留GameDataLoading对象的引用?

例如,以下代码不起作用。但是,如果您将GameDataLoading()替换为loading = GameDataLoading(),则可行。

from threading import Thread
from wx.lib.pubsub import pub
import wx

Publisher = pub.Publisher()

class GameDataLoading(object):
    def __init__(self):
        self.view = wx.Frame(None)
        self.view.Center()
        self.view.Show()

        Publisher().subscribe(self.test, 'test')
        LoadGameData().start()

    def test(self, data):
        print 'working'

class LoadGameData(Thread):
    def __init__(self):
        super(LoadGameData, self).__init__()

    def run(self):
        wx.CallAfter(Publisher().sendMessage, 'test')

app = wx.PySimpleApp()
GameDataLoading()
app.MainLoop()