【问题标题】:Using PubNub to publish message in Django view使用 PubNub 在 Django 视图中发布消息
【发布时间】:2018-08-20 04:17:32
【问题描述】:

我想使用 PubNub 从 Django 视图发布消息。我在 pythonanywhere 上运行我的网络应用程序。起初它似乎工作正常,但如果我连续刷新页面几次,我最终会捕获一个带有消息“无法启动新线程”的 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

感谢您的帮助。

【问题讨论】:

  • 添加完整的错误回溯

标签: python django pubnub pythonanywhere


【解决方案1】:

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

Valid PubNub Channel Names

这段代码应该可以工作:

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)

【讨论】:

    【解决方案2】:

    在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多