繁体   English   中英

使用PubNub在Django视图中发布消息

[英]Using PubNub to publish message in Django view

我想使用PubNub从Django视图发布消息。 我在pythonanywhere上运行我的Web应用程序。 乍一看似乎工作正常,但是如果我连续刷新页面几次,最终会捕获到RuntimeError异常,并显示消息“无法启动新线程”。 如果我稍等(不确定时间),它将重新开始工作,然后重复相同的行为。

似乎问题在于为回调启动了一个新线程,但是我不确定。

这是我的视图和views.py中的回调的代码:

def my_view(request):
    try:
        pnconfig = PNConfiguration()

        pnconfig.subscribe_key = '<sub key>'
        pnconfig.publish_key = '<pub key>'
        pnconfig.ssl = False

        pubnub = PubNub(pnconfig)
        pubnub.publish().channel('MyDemoChannel').message({"text": "Message from my_view()"}).async(publish_callback)
    except:
        logger.exception("Caught exception in my_view()")

        context = {'val_1': 1, 'val_2': 2}
        return render( request, 'my_app/my_view.html', context)


def publish_callback(envelope, status):
    if not status.is_error():
        pass
    else:
        pass

和堆栈跟踪:

[25/Aug/2018 11:30:25] ERROR [league.views:42] Caught exception in my_view()
Traceback (most recent call last):
  File "/home/tennis/tennis.pythonanywhere.com/website/league/views.py", line 40, in my_view
    pubnub.publish().channel('MyDemoChannel').message({"text": "Message from my_view()"}).async(publish_callback)
  File "/home/tennis/.virtualenvs/tennis.pythonanywhere.com/lib/python3.6/site-packages/pubnub/endpoints/endpoint.py", line 116, in async
    cancellation_event=self._cancellation_event)
  File "/home/tennis/.virtualenvs/tennis.pythonanywhere.com/lib/python3.6/site-packages/pubnub/pubnub.py", line 68, in request_async
    callback, cancellation_event)
  File "/home/tennis/.virtualenvs/tennis.pythonanywhere.com/lib/python3.6/site-packages/pubnub/request_handlers/requests_handler.py", line 81, in async_request
    thread.start()
  File "/usr/lib/python3.6/threading.py", line 846, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread

谢谢你的帮助。

有2个问题。 首先,您需要在发布行中添加.async(callback_function_here) 另外,您的频道名称包含空格,这是无效的。 频道名称限制在此处列出:

有效的PubNub频道名称

此代码应工作:

from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

pnconfig = PNConfiguration()

pnconfig.subscribe_key = '<sub key>'
pnconfig.publish_key = '<pub key>'
pnconfig.ssl = False

pubnub = PubNub(pnconfig)

def publish_callback(result, status):
    pass
    # Handle PNPublishResult and PNStatus

pubnub.publish().channel('MyDemoChannel').message({"text": "Message from my_view()"}).async(publish_callback)

在my_view()外部配置和实例化PubNub对象可解决此问题:

from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

pnconfig = PNConfiguration()

pnconfig.subscribe_key = '<sub key>'
pnconfig.publish_key = '<pub key>'
pnconfig.ssl = False

pubnub = PubNub(pnconfig)

def my_view(request):
    try:
        pubnub.publish().channel('MyDemoChannel').message({"text": "Message from my_view()"}).async(publish_callback)
    except:
        logger.exception("Caught exception in my_view()")

        context = {'val_1': 1, 'val_2': 2}
        return render( request, 'my_app/my_view.html', context)


def publish_callback(envelope, status):
    if not status.is_error():
        pass
    else:
        pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM