python - 不能通过异步调用的函数加入Python调试器吗?

为了调试Airbrake throwing error "pybrake - ERROR - strconv.ParseInt: parsing "None": invalid syntax"中描述的Airbrake问题,我尝试在将请求发送到Airbrake之前检查请求,方法是使用import ipdb; ipdb.set_trace()进入iPython调试器。
为了检查请求,我在send_notic_sync()Notifier方法中设置了一个跟踪(请参见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

此方法将提交到ThreadPoolExecutor的源代码中的pybrake。问题是,当我试图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设置。

本文翻译自 https://stackoverflow.com/questions/50631690/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

标签 python django asynchronous ipdb airbrake


相关文章:

python - 在Django中打印对象

python - Python-运行进程并等待输出

python - 为Django模型表单集设置label_suffix

python - 当数据库中已经存在数据时如何减少Django中CharField的max_length

python - 为什么trollius不使该循环异步运行?

python - 从内置类继承是否正确?

php - 对于Python,什么是PHP的json_encode [重复]

python - 在django-taggit中,如何获取与特定用户关联的对象的标签?

c# - 异步调用API并解析JSON

ios - Alamofire异步请求在for循环中发生故障