代码之家  ›  专栏  ›  技术社区  ›  Krupa Suthar

pubnub asyncio不发布数据

  •  4
  • Krupa Suthar  · 技术社区  · 6 年前

    我在用 PubNub Asyncio 库发布数据,下面是我的代码。

    import asyncio
    import os
    
    from pubnub.exceptions import PubNubException
    from pubnub.pnconfiguration import PNConfiguration
    from pubnub.pubnub_asyncio import PubNubAsyncio
    
    def publish_callback(result, status):
        print(status)
    
    class PubNubIntegrator:
        def __init__(self):
            print("In initialize")
            pn_configuration = PNConfiguration()
            pn_configuration.publish_key = os.environ['PUB_KEY']
            pn_configuration.subscribe_key = os.environ['SUB_KEY']
            pn_configuration.ssl = True
            self.pubnub = PubNubAsyncio(pn_configuration)
    
    
        async def publish_data(self, message, channel_name):
            print('message on channel {0}'.format(channel_name))
            try:
                asyncio.ensure_future(self.pubnub.publish().channel(channel_name).message(message).future()).add_done_callback(publish_callback)
            except PubNubException as e:
                print("Error while publishing data", message)
    

    我从另一个函数调用它,

    async def stream(self):
            result = { "mesage": "data"} 
            pn_integrator = PubNubIntegrator()
            await pn_integrator.publish_data(result, 'demo_channel')
    

    但它不起作用,也不能将数据发布到channel。

    0 回复  |  直到 6 年前