运行时错误:此事件循环已在 python 中运行 [英] RuntimeError: This event loop is already running in python

查看:53
本文介绍了运行时错误:此事件循环已在 python 中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我收到这个错误是因为我的代码调用了 asyncio.get_event_loop().run_until_complete(foo()) 两次.一次来自 foo(),第二次来自 foo() 调用的函数.我的问题是:为什么这会成为一个问题?为什么我还要关心这个循环是否正在运行?

I think I'm getting this error because my code calls asyncio.get_event_loop().run_until_complete(foo()) twice. Once from foo() and second time from function called by foo(). My question is then: why should this be a problem? Why should I even care that this loop is running?

对这个问题进行了编辑,我认为它掩盖了它(有些人喜欢在不理解规则的情况下遵守规则,因此从标题中删除了非法"一词).不幸的是,这会造成混淆.

There was an edit made to this question which, I think, obscured it (some people prefer to follow rules without understanding them, thus an "illegal" word was removed from the title). Unfortunately, this creates confusion.

我对出现错误这一事实并不感到惊讶.我可以追溯到 asyncio 源代码,并看到该库的作者想要这样做,这并不神秘.令人费解的部分是该库的作者认为在循环已经运行的情况下从事件循环中要求运行某个函数直到完成是非法的.

I'm not surprised by the fact that the error is raised. I can trace it back to the asyncio source and see that the authors of this library wanted to do it this way, there's no mystery there. The puzzling part is in the reason the authors of the library decided it's illegal to ask from event loop to run some function to completion when the loop is already running.

我们可以将问题简化为两个这样的调用,通过案例分析我们将看到这三种可能性:

We can reduce the problem to just two such calls, and through case analysis we will see that these are the three possibilities:

  1. 这两个函数都不会终止.
  2. 其中一个函数最终终止.
  3. 这两个函数最终都会终止.

现在,是否有任何理智的行为可以解决所有三种情况?对我来说,很明显,这里可能存在,或者,可能存在多种理智的行为.例如:

Now, is there any sane behavior which would address all three cases? To me, it is obvious that there is, or, perhaps are multiple sane behaviors possible here. For example:

  1. 没什么特别的,这两个函数的执行是交错的,而且它们会一直运行下去,正如预期的那样.
  2. 循环不会将控制权返回给 run_until_complete() 第一个实例之后的代码,直到第二个函数完成(因此 run_until_complete() 之后的代码将不会被执行.
  3. 在最后一个函数终止后,循环将控制权返回给调用 run_until_complete 的第一个代码对象,忽略所有其他调用站点.
  1. Nothing special, the execution of both functions is interleaved, and they keep running forever, just as expected.
  2. The loop doesn't return control to the code following the first instance of run_until_complete() until second function completes (thus no code after run_until_complete() will be executed.
  3. After the last function terminates, the loop returns control to the first code object which invoked run_until_complete ignoring all other invocation sites.

现在,我可以理解这种行为可能不是每个人都想要的.但是,由于该库决定让程序员控制启动/停止事件循环,因此它也应该满足此类决定的后果.多次启动同一个循环成为错误会阻止库代码执行此操作,这会降低使用 asyncio 的库的质量和实用性(例如, 确实是这种情况)aiohttp).

Now, I can understand that this behavior may not be something that everyone would want. But, since this library decided to give programmers control over starting / stopping the event loop, it should also meet the consequences of such decisions. Making it an error to start the same loop multiple times precludes library code from ever doing this, which reduces the quality and usefulness of libraries utilizing asyncio (which is indeed the case with, for example, aiohttp).

推荐答案

我使用 nest_async 解决了这个问题

I got the issue resolved by using the nest_async

pip install nest-asyncio

并在我的文件中添加以下几行.

and adding below lines in my file.

import nest_asyncio
nest_asyncio.apply()
__import__('IPython').embed()

这篇关于运行时错误:此事件循环已在 python 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆