我对python中所有异步的东西都很陌生。我想在异步松弛RTM客户端使用专用回调监听消息时运行某些代码,如下所示:
RTM_CLIENT.start()
while True:
...
except Exception as e:
...
finally:
RTM_CLIENT.stop()回调函数:
@slack.RTMClient.run_on(event='message')
def listen(**payload):
...RTM_CLIENT.start()函数返回一个future对象。不过,我没有收到任何消息事件。我做错了什么吗?
发布于 2019-06-16 16:44:32
这就解决了这个问题(线程同步):
import re
import slack
import time
import asyncio
import concurrent
from datetime import datetime
@slack.RTMClient.run_on(event='message')
async def say_hello(**payload):
data = payload['data']
print(data.get('text'))
def sync_loop():
while True:
print("Hi there: ", datetime.now())
time.sleep(5)
async def slack_main():
loop = asyncio.get_event_loop()
rtm_client = slack.RTMClient(token='x', run_async=True, loop=loop)
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
await asyncio.gather(
loop.run_in_executor(executor, sync_loop),
rtm_client.start()
)
if __name__ == "__main__":
asyncio.run(slack_main())https://stackoverflow.com/questions/56539228
复制相似问题