2024-05-16 00:21:16 发布
网友
我对python中所有异步的东西都很陌生。在异步slack RTM客户端使用专用回调监听消息时,我想运行一段特定的代码,如下所示:
RTM_CLIENT.start() while True: ... except Exception as e: ... finally: RTM_CLIENT.stop()
回调函数:
RTM_CLIENT.start()函数返回一个future对象。 但我没有收到任何消息事件。我做错什么了吗?在
RTM_CLIENT.start()
future
这就解决了这个问题(线程同步):
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())
这就解决了这个问题(线程同步):
相关问题 更多 >
编程相关推荐