簡體   English   中英

Gevent從錯誤的線程拋出異常(greenlet)

[英]Gevent throws exception from wrong thread (greenlet)

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 中生成一個新gevent.pool.Pool ,據我所知,它不應該進入網絡。 我的意思是生成greenlet的行為不應該影響網絡,盡管最終將在該greenlet中運行的function可能會影響網絡,但我認為我不需要在這里擔心這一點,因為沒有證據表明它在回溯中。

為什么 gevent 會在代碼的這個位置引發這個看似無關的網絡錯誤? 我懷疑錯誤來自另一個正在訪問網絡的greenlet。 有沒有辦法為這個錯誤獲取真正的回溯上下文?

不確定以下任何一項是否相關,但對於上下文:

在啟動時,該過程會修補一些東西。 這有點簡化( 此處的實際代碼):

from gevent import monkey
from psycogreen.gevent import patch_psycopg

monkey.patch_all(subprocess=True)
patch_psycopg()

編輯: 稍后它會設置gevent.get_hub().SYSTEM_ERROR = BaseException以使程序在任何 greenlet 崩潰時立即退出。 也許這還有其他意想不到的副作用,例如這些令人困惑的回溯?

系統和庫版本:

  • Linux djangomanage1-production 4.15.0-1041-aws #43-Ubuntu SMP Thu Jun 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

確實,看起來

gevent.get_hub().SYSTEM_ERROR = BaseException

有意想不到的副作用。 socket.gaierror是斷斷續續的,但相當頻繁,自從我注釋掉該行以來就沒有發生過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM