如何禁用Python RQ作业的SyntaxWarning?

3
尝试避免RQ显示警告消息(到控制台),关于使用meta字典进行任意属性使用。我们正在按照指定方式使用它,但警告仍在显示。 显示的警告如下:
/usr/local/lib/python2.7/site-packages/rq/job.py:381: SyntaxWarning: Getting custom properties from the job instance directly will be unsupported as of RQ 0.4. Please use the meta dict to store all custom variables.  So instead of this:

 job.foo

Use this:

 job.meta['foo']

SyntaxWarning)

基本上,这很烦人,因为它会干扰正常的调试活动。
有什么办法可以禁用它吗?
1个回答

1
使用内置的 warnings 模块的 simplefilter 方法。需要使用上下文管理器。代码示例从链接部分整体复制:
import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

进一步的参数可以传递给simplefilter,让你过滤掉你已知代码中特定位置产生的警告 - 这可能是一个好主意,这样其他新的警告不会被掩盖。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接