在pubnub回调函数中打印未显示

2024-06-24 13:47:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python测试pubnub API,在订阅回调函数中打印消息时遇到问题,新消息出现时会触发订阅回调函数

在下面的代码中,接收到的消息以某种方式排队并仅打印 当某些消息在之后发送时。一切都会立即执行, 只有实际打印延迟

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

import os
import logging

CHANNEL = 'channel1'
USER = 'user'

pnconfig = PNConfiguration()
pnconfig.publish_key = ""
pnconfig.subscribe_key = ""
pnconfig.uuid = "serverUUID-PUB"

pnb = PubNub(pnconfig)

class ReceiverCallback(SubscribeCallback):

    def message(self, pnb, event):
        if event.message['text'] == 'exit':
            os._exit(0)
        print(logging.info(event.message['text'])) # this doesn't show

pnb.add_listener(ReceiverCallback())
pnb.subscribe().channels(CHANNEL).execute()

while True:
    txt = input('')
    pnb.publish().channel(CHANNEL).message({
        'from': USER,
        'text': txt
    }).sync()
    print(txt) # this shows

如果我用logging.warn替换print,并在循环前设置pubnub.set_stream_logger('pubnub', logging.INFO,30),则消息显示为ERROR:root:<message>, 但是logging.infologging.debug不适用于同样的情况 通过API的实现进行日志记录

我不认为我从中获得代码(基本相同)的教程没有提到这一点。 我也没有发现其他地方提到过这个问题

编辑:这只是在从git bash运行脚本时出现的问题,而不是从idle或Powershell运行脚本时出现的问题


Tags: 函数textfromimporttxteventapi消息