为什么我收到“运行时错误:此事件循环已在运行”

mcp*_*ven 2 python python-3.x python-decorators slack-api

我正在使用新的 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()
Run Code Online (Sandbox Code Playgroud)

它在启动时输出机器人 ID(使用 ) ,但当我再次调用 时webclient崩溃。RuntimeError: This event loop is already runningwebclient

[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
Run Code Online (Sandbox Code Playgroud)

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

#!/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()
Run Code Online (Sandbox Code Playgroud)
[root@slackbot-01 bin]# scl enable rh-python36 /root/slackbot/bin/slackbot.py
True
True
^C[root@slackbot-01 bin]#
Run Code Online (Sandbox Code Playgroud)

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

我在这里做错了什么?

sco*_*gen 5

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

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

import nest_asyncio
nest_asyncio.apply()
Run Code Online (Sandbox Code Playgroud)

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