无法在异步调用的函数中放入 Python 调试器?



为了调试空气制动器抛出错误"pybrake - 错误 - strconv.ParseInt:解析"无":语法无效",我正在尝试通过使用import ipdb; ipdb.set_trace()放入 iPython 调试器来检查请求之前将它们发送到 Airbrake。

为了检查请求,我在Notifiersend_notic_sync()方法中设置了跟踪(请参阅 https://github.com/airbrake/pybrake/blob/master/pybrake/notifier.py(:

def send_notice_sync(self, notice):
"""Sends notice to Airbrake.
It returns notice with 2 possible new keys:
- {'id' => str} - notice id on success.
- {'error' => str|Exception} - error on failure.
"""
for fn in self._filters:
r = fn(notice)
if r is None:
notice['error'] = 'notice is filtered out'
return notice
notice = r
if time.time() < self._rate_limit_reset:
notice['error'] = _ERR_IP_RATE_LIMITED
return notice
data = jsonify_notice(notice)
req = urllib.request.Request(self._airbrake_url,
data=data,
headers=self._airbrake_headers)
try:
import ipdb; ipdb.set_trace()
resp = urllib.request.urlopen(req, timeout=5)
except urllib.error.HTTPError as err:
resp = err
except Exception as err: # pylint: disable=broad-except
notice['error'] = err
logger.error(notice['error'])
return notice

此方法被提交到pybrake源代码中的ThreadPoolExecutor。问题是,当我尝试import调用此函数的脚本时,我无法放入调试器。这是我尝试时看到的:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py shell
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import lucy_web.tests.test_airbrake
In [2]: ipdb>                                                                                                       
2018-05-31 11:52:14,155 - pybrake - ERROR - 
> /Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/pybrake/notifier.py(119)send_notice_sync()
118       import ipdb; ipdb.set_trace()
--> 119       resp = urllib.request.urlopen(req, timeout=5)
120     except urllib.error.HTTPError as err:
ipdb> ^[ipdb>                                                                                                       
In [2]: 2018-05-31 11:52:14,159 - pybrake - ERROR - 
^[[37;1R> /Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/pybrake/notifier.py(119)send_notice_sync()
118       import ipdb; ipdb.set_trace()
--> 119       resp = urllib.request.urlopen(req, timeout=5)
120     except urllib.error.HTTPError as err:
^[[37;1R
^[[37;1RIn [2]: dir()
In [2]: dir()
Out[2]: 
['In',
'Out',
'_',
'__',
'___',
'__builtin__',
'__builtins__',
'__doc__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_dh',
'_i',
'_i1',
'_i2',
'_ih',
'_ii',
'_iii',
'_oh',
'exit',
'get_ipython',
'lucy_web',
'quit']

因此,尽管调试器的输出有"闪烁",但最终我只是在原始ipdb会话的范围内结束。如何做到这一点,以便我可以在send_notice_sync()方法中设置跟踪?

我最终通过在Notifier__init__()方法中闯入调试器来解决此问题,该方法之所以有效,是因为它不是异步调用的。在这里,我能够确定project_id正在None传入,因为我没有正确设置我的 DjangoAIRBRAKE设置。

相关内容

  • 没有找到相关文章

最新更新