我正在使用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')
但是它不起作用,也没有将数据发布到频道。