gpt4 book ai didi

python - 为什么我收到 "RuntimeError: This event loop is already running"

转载 作者:行者123 更新时间:2023-12-02 19:55:33 26 4
gpt4 key购买 nike

我正在使用新的 slack 2.0 python 库开发一个 slack 机器人。我是 python 装饰器的新手,我怀疑这是我的问题的一部分。

这是我的代码...

#!/opt/rh/rh-python36/root/usr/bin/python
import os
import slack

# instantiate Slack client
slack_token = os.environ['SLACK_BOT_TOKEN']
rtmclient = slack.RTMClient(token=slack_token)
webclient = slack.WebClient(token=slack_token)

# get the id of my user
bot_id = webclient.auth_test()['user_id']
print('Bot ID: {0}'.format(bot_id))

def get_user_info(user_id):
user_info = webclient.users_info(user=user_id)['ok']
return user_info

@slack.RTMClient.run_on(event='message')
def parse_message(**payload):
data = payload['data']
user_id = data['user']
print(get_user_info(user_id))

rtmclient.start()

它在启动时输出机器人 ID(使用 webclient),但当我再次调用 时,它会因 RuntimeError:此事件循环已在运行而崩溃网络客户端

[root@slackbot-01 bin]# scl enable rh-python36 /root/slackbot/bin/slackbot.py
Bot ID: UBT547D31
Traceback (most recent call last):
File "/root/slackbot/bin/slackbot.py", line 24, in <module>
rtmclient.start()
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/slack/rtm/client.py", line 197, in start
return self._event_loop.run_until_complete(future)
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/asyncio/base_events.py", line 467, in run_until_complete
return future.result()
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/slack/rtm/client.py", line 339, in _connect_and_read
await self._read_messages()
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/slack/rtm/client.py", line 390, in _read_messages
await self._dispatch_event(event, data=payload)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/slack/rtm/client.py", line 440, in _dispatch_event
self._execute_in_thread(callback, data)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/slack/rtm/client.py", line 465, in _execute_in_thread
future.result()
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/concurrent/futures/_base.py", line 425, in result
return self.__get_result()
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/root/slackbot/bin/slackbot.py", line 22, in parse_message
print(get_user_info(user_id))
File "/root/slackbot/bin/slackbot.py", line 15, in get_user_info
user_info = webclient.users_info(user=user_id)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/slack/web/client.py", line 1368, in users_info
return self.api_call("users.info", http_verb="GET", params=kwargs)
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/slack/web/base_client.py", line 154, in api_call
return self._event_loop.run_until_complete(future)
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/asyncio/base_events.py", line 454, in run_until_complete
self.run_forever()
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/asyncio/base_events.py", line 408, in run_forever
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

对我来说真正令人困惑的部分是,如果我注释掉第一次调用 webclient.auth_test() 的行,我就没有任何问题。每次 rtmclient 向我发送数据时,我对 webclient.users_info() 的调用都会起作用。

#!/opt/rh/rh-python36/root/usr/bin/python
import os
import slack

# instantiate Slack client
slack_token = os.environ['SLACK_BOT_TOKEN']
rtmclient = slack.RTMClient(token=slack_token)
webclient = slack.WebClient(token=slack_token)

# get the id of my user
#bot_id = webclient.auth_test()['user_id']
#print('Bot ID: {0}'.format(bot_id))

def get_user_info(user_id):
user_info = webclient.users_info(user=user_id)['ok']
return user_info

@slack.RTMClient.run_on(event='message')
def parse_message(**payload):
data = payload['data']
user_id = data['user']
print(get_user_info(user_id))

rtmclient.start()
[root@slackbot-01 bin]# scl enable rh-python36 /root/slackbot/bin/slackbot.py
True
True
^C[root@slackbot-01 bin]#

我需要获取机器人 ID,以便确保它不会回复自己的消息。我不明白为什么我的代码在使用装饰器在解析消息函数之外获取机器人 ID 后不起作用。

我在这里做错了什么?

最佳答案

Python 事件循环对于库编程来说是一件棘手的事情,并且在 SlackClient 2.0 版本中管理事件队列的方式存在一些问题。看起来 2.1 做了一些改进,但它似乎是一项正在进行的工作,我仍然遇到这个问题。我预计 future 会有更新以使其更加强大。

与此同时,文件顶部的以下代码(使用 pip 进行安装)通常可以帮我解决这个问题:

import nest_asyncio
nest_asyncio.apply()

请记住,这将改变应用程序其余部分处理事件队列的方式(如果这是一个因素)。

关于python - 为什么我收到 "RuntimeError: This event loop is already running",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154308/

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