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

时间:2019-10-01 20:07:43

标签: python gevent

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,据我所知,它不应进入网络。我的意思是,产生绿色角色的行为不应影响网络,尽管最终将在该绿色角色中运行的功能可能会影响网络,但是我认为我不必为此担心在这里,因为在追溯中没有任何证据。

为什么gevent在代码中的这个地方引发了这个看似无关的网络错误?我怀疑该错误来自 正在访问网络的另一个绿帽。有没有办法获取此错误的真实回溯上下文?

不确定以下任何一项是否相关,但取决于上下文:

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

from gevent import monkey
from psycogreen.gevent import patch_psycopg

monkey.patch_all(subprocess=True)
patch_psycopg()

编辑:a bit later设置gevent.get_hub().SYSTEM_ERROR = BaseException以使如果任何greenlet崩溃,程序都会立即退出。也许还有其他意外的副作用,例如这些令人困惑的回溯?

系统和库版本:

  • Linux djangomanage1-production 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
  • greenlet 0.4.15

1 个答案:

答案 0 :(得分:0)

确实,确实如此

gevent.get_hub().SYSTEM_ERROR = BaseException

具有意外的副作用。自从我注释掉该行以来,socket.gaierror断断续续但相当频繁。