我有一个Django项目,其中包含一个带有ready
方法的应用程序。我使用ready方法创建一个Pubnub侦听器对象。代码看起来像这样:
# myproject/myapp/apps.py
from pubnub.callbacks import SubscribeCallback
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from django.apps import AppConfig
from somewhere import SomeNamespace
class MySubscribeCallback(SubscribeCallback):
def message(self, pubnub, message):
# store the message in the database
class MyPubnub(PubNub):
def __init__(self):
pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'demo'
pnconfig.publish_key = 'demo'
super(MyPubnub, self).__init__(config=pnconfig)
self.add_listener(MySubscribeCallback())
self.subscribe().channels('demo').execute()
class MyAppConfig(AppConfig):
name = 'myapp'
def ready(self):
SomeNamespace.my_pubnub_listener = MyPubnub()
存储my_pubnub_listener
的适当命名空间在哪里?如果我把它放在self.my_pubnub_listener
中,它会在启动后清理吗?它有更好的地方吗?