Python Jupyter、asyncio和Slack

Python Jupyter、asyncio和Slack,python,jupyter-notebook,python-asyncio,slack,Python,Jupyter Notebook,Python Asyncio,Slack,我面临一个已知的问题,即在Jupyter(,和中的引用)中运行异步感知函数 我刚刚将Slack Python库更新到2.0版,我面临与上面相同的问题,即 import slack client = slack.WebClient(token=os.environ["SLACK_TOKEN"]) client.chat_postMessage(channel="@flavien", text="test") -------------------------------------------

我面临一个已知的问题,即在Jupyter(,和中的引用)中运行异步感知函数

我刚刚将Slack Python库更新到2.0版,我面临与上面相同的问题,即

import slack

client = slack.WebClient(token=os.environ["SLACK_TOKEN"])
client.chat_postMessage(channel="@flavien", text="test")

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-12-20f89b3af945> in <module>
      2 
      3 client = slack.WebClient(token=os.environ["SLACK_TOKEN"])
----> 4 asyncio.run(client.chat_postMessage(channel="@flavien", text="test"))

~/miniconda3/envs/analytics/lib/python3.6/site-packages/slack/web/client.py in chat_postMessage(self, channel, **kwargs)
    289         """
    290         kwargs.update({"channel": channel})
--> 291         return self.api_call("chat.postMessage", json=kwargs)
    292 
    293     def chat_unfurl(

~/miniconda3/envs/analytics/lib/python3.6/site-packages/slack/web/base_client.py in api_call(self, api_method, http_verb, files, data, params, json)
    133             return future
    134 
--> 135         return self._event_loop.run_until_complete(future)
    136 
    137     def _validate_xoxp_token(self):

~/miniconda3/envs/analytics/lib/python3.6/site-packages/nest_asyncio.py in run_until_complete(self, future)
     61             return f.result()
     62         else:
---> 63             return self._run_until_complete_orig(future)
     64 
     65     bogus_handle = asyncio.Handle(None, None, loop)

~/miniconda3/envs/analytics/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
    453         future.add_done_callback(_run_until_complete_cb)
    454         try:
--> 455             self.run_forever()
    456         except:
    457             if new_task and future.done() and not future.cancelled():

~/miniconda3/envs/analytics/lib/python3.6/asyncio/base_events.py in run_forever(self)
    410         if events._get_running_loop() is not None:
    411             raise RuntimeError(
--> 412                 'Cannot run the event loop while another loop is running')
    413         self._set_coroutine_wrapper(self._debug)
    414         self._thread_id = threading.get_ident()

RuntimeError: Cannot run the event loop while another loop is running

提前感谢您的帮助。

据我所知,问题是您正在您的
jupyter
单元格或
ipython
中运行这段代码。尝试将其作为独立的python代码运行,如果您看到相同的错误,请告诉我


您看到此错误的原因是,
jupyter
已经为您的单元运行了一个内部驱动内核的循环

您可以尝试的一种解决方法是在单独的进程中运行相关代码:

导入松弛
从多处理导入进程
def main():
client=slack.WebClient(token=os.environ[“slack\u token”])
client.chat_postMessage(channel=“@flavien”,text=“test”)
p=进程(目标=主,参数=())
p、 开始()
p、 加入
但是,根据我的测试,如果底层代码使用
loop=asyncio.get\u event\u loop()
则检索到的循环将是在Jupyter上运行的循环(这可能会导致一些不必要的行为)


如果基础代码使用
asyncio.run(coro())
或使用带有
loop=asyncio.new\u event\u loop()
的新循环,则可以避免此问题,谢谢您的回复。不过我不明白。如果我理解正确,就需要获取现有的循环,而不是尝试开始一个新的循环。但是我不能这样做,因为启动是由
slack
模块管理的,对吗?我认为这正是嵌套循环的关键所在。
jupyter                   1.0.0                      py_2    conda-forge
ipykernel                 5.1.0         py36h24bf2e0_1002    conda-forge
tornado                   6.0.2            py36h516909a_0    conda-forge
slackclient               2.0.1                    pypi_0    pypi