python - Jupyter、asyncio 和 Slack

标签 python jupyter-notebook python-asyncio slack

我在 Jupyter 中运行异步感知函数时面临一个已知问题( https://github.com/jupyterlab/jupyterlab/issues/4399https://github.com/ipython/ipython/issues/11030https://blog.jupyter.org/ipython-7-0-async-repl-a35ce050f7f7 中的引用)。

我刚刚将 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

尽管进行了很多尝试,我还是没能让它运行起来。有什么办法可以避免这个问题吗?

我的 Python 3.6.6 软件包版本

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

预先感谢您的帮助。

最佳答案

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

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

关于python - Jupyter、asyncio 和 Slack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069975/

相关文章:

python - 读取为整数数组的数字图像

Python AST 节点转换器 : Return multiple nodes

python - Django:检查对象是否最后一个链接到另一个

python - 权限错误: [Errno 13] Permission denied when accessing to aws ec2

python - 将 asyncio 与 tkinter 一起使用,它是 ttk.progressbar 小部件 : how to force an asyncio task to end?

python - 计算numpy数组中点内的欧氏距离

ubuntu - 用pip安装后, "jupyter: command not found"

jupyter-notebook - 让 Altair 与 Jupyter Notebook 一起工作

python-3.x - ValueError ("I/O operation on closed pipe") 将 asyncio 与子进程一起使用时

python - 如何进行异步函数的单元测试?