gpt4 book ai didi

python - slackclient python - 异步 RTM 客户端

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:47 25 4
gpt4 key购买 nike

我对 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 对象。我没有收到任何消息事件。我做错了什么吗?

最佳答案

这解决了(线程同步):

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())

关于python - slackclient python - 异步 RTM 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56539228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com