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

时间:2019-07-22 22:02:58

标签: python python-3.x slack-api python-decorators

我正在使用新的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),但是当我再次调用RuntimeError: This event loop is already running时,它会与webclient崩溃。

[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()的那一行,那我就没有问题了。每当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()
[root@slackbot-01 bin]# scl enable rh-python36 /root/slackbot/bin/slackbot.py
True
True
^C[root@slackbot-01 bin]#

我需要获取机器人ID,以便确保它不会回答自己的消息。我不明白为什么在使用装饰器将bot ID解析到解析消息功能之外之后,代码仍无法正常工作的原因。

我在这里做什么错了?

2 个答案:

答案 0 :(得分:0)

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

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

import nest_asyncio
nest_asyncio.apply()

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

答案 1 :(得分:0)

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