Gevent从错误的线程抛出异常(greenlet)

2024-06-26 14:20:37 发布

您现在位置:Python中文网/ 问答频道 /正文

Gevent似乎在当前未访问网络的greenlet中引发了与网络相关的错误。下面是一个示例回溯,为了简洁起见,省略了一些细节:

Traceback (most recent call last):
  ...
  File ".../asyncforms.py", line 95, in _try_to_process_form
    self.pool.spawn(self._async_migrate_form, wrapped_form, case_ids)
  File ".../lib/python3.6/site-packages/gevent/pool.py", line 391, in spawn
    self.start(greenlet)
  File ".../lib/python3.6/site-packages/gevent/pool.py", line 601, in start
    self.add(greenlet, *args, **kwargs)
  File ".../lib/python3.6/site-packages/gevent/pool.py", line 634, in add
    if not self._semaphore.acquire(blocking=blocking, timeout=timeout):
  File "src/gevent/_semaphore.py", line 100, in gevent.__semaphore.Semaphore.acquire
  File "src/gevent/_semaphore.py", line 128, in gevent.__semaphore.Semaphore.acquire
  File "src/gevent/_abstract_linkable.py", line 192, in gevent.__abstract_linkable.AbstractLinkable._wait
  File "src/gevent/_abstract_linkable.py", line 165, in gevent.__abstract_linkable.AbstractLinkable._wait_core
  File "src/gevent/_abstract_linkable.py", line 169, in gevent.__abstract_linkable.AbstractLinkable._wait_core
  File "src/gevent/_greenlet_primitives.py", line 60, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
  File "src/gevent/_greenlet_primitives.py", line 60, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
  File "src/gevent/_greenlet_primitives.py", line 64, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
  File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
socket.gaierror: [Errno -9] Address family for hostname not supported

正如您所看到的,当前线程是gevent.pool.Pool中的spawning a new greenlet,据我所知,它不应该访问网络。我的意思是,生成一个greenlet的行为不应该击中网络,虽然最终在该greenlet中运行的函数可能会击中网络,但是我认为我不需要在这里担心,因为在回溯中没有它的证据。你知道吗

为什么gevent在代码中的这个地方提出这个看似无关的网络错误?我怀疑错误来自另一个正在访问网络的greenlet。有没有办法获取此错误的真实回溯上下文?你知道吗

不确定以下任何一项是否相关,但与上下文有关:

启动时,进程会修补一些东西。这有点简化(实际代码here):

from gevent import monkey
from psycogreen.gevent import patch_psycopg

monkey.patch_all(subprocess=True)
patch_psycopg()

Edit:a bit later它设置gevent.get_hub().SYSTEM_ERROR = BaseException,使程序在任何greenlet崩溃时立即退出。也许这还有其他意想不到的副作用,比如这些令人困惑的回溯?你知道吗

系统和库版本:

  • Linux djangomanage1产品4.15.0-1041-aws#43 Ubuntu SMP周四6月6日13:39:11 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  • Python 3.6.8版
  • gevent 1.4.0版本
  • 绿色0.4.15

Tags: inpyself网络srcabstract错误line
1条回答
网友
1楼 · 发布于 2024-06-26 14:20:37

的确,看起来

gevent.get_hub().SYSTEM_ERROR = BaseException

有意想不到的副作用。socket.gaierror,这是间歇性的,但相当频繁,没有发生,因为我评论了这一行。你知道吗

相关问题 更多 >