slackclient python - 异步 RTM 客户端

slackclient python - async RTM client

我对 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()函数returns一个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())