【问题标题】:Why am I getting "RuntimeError: This event loop is already running"为什么我会收到“RuntimeError:此事件循环已在运行”
【发布时间】:2019-11-30 22:02:59
【问题描述】:

我正在使用新的 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()

它在启动时输出 Bot ID(使用 webclient),但当我再次调用 webclient 时,它会以 RuntimeError: This event loop is already running 崩溃。

[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 python-3.x slack-api python-decorators


    【解决方案1】:

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

    与此同时,您文件顶部的以下代码(使用 pip 安装)通常会为我解决它:

    import nest_asyncio
    nest_asyncio.apply()
    

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

    【讨论】:

      【解决方案2】:

      如果您使用的是 RTM,RTMClient 会为您创建一个 WebClient。当您处理事件时,它的句柄应该在有效负载中传递给您。您可以通过查找始终在 RTM 成功连接后调度的“打开”事件并在您的“打开”事件处理程序中进行查找来检查您的 ID。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-24
        • 1970-01-01
        • 2019-02-09
        相关资源
        最近更新 更多