celery的源码local.py
中有这么一个类Proxy
,作者说是Code stolen from werkzeug.local.Proxy.
,我核对了一下,代码确实是一样的,其中有这么一个方法:
def _get_current_object(self):
"""Return the current object. This is useful if you want the real
object behind the proxy at a time for performance reasons or because
you want to pass the object into a different context."""
loc = object.__getattribute__(self, '_Proxy__local')
if not hasattr(loc, '__release_local__'):
return loc(*self.__args, **self.__kwargs)
try: # pragma: no cover
# not sure what this is about
return getattr(loc, self.__name__)
except AttributeError: # pragma: no cover
raise RuntimeError('no object bound to {0.__name__}'.format(self))
注释说这个方法有助于性能提升,还可以在不同的context中的传递对象。
我想问一下,这个函数或者说用Proxy提升性能的原理是什么?
望不吝赐教,谢谢!
ここでの質問の理解には問題があるかもしれません。著者はパフォーマンスの向上についてではなく、パフォーマンスの理由について話しました。そのため、
Proxy
を使用すると、アプリケーションはスレッド分離エージェントになります。Proxy
などの対応するオブジェクトを使用する必要があります。プロキシ オブジェクトを取得するには、オーバーヘッドが必要です。current_app
メソッドを呼び出して実際のオブジェクトを取得する場合、使用時にプロキシ オーバーヘッドは発生しません。この場合、_get_current_object
はパフォーマンス上の理由から呼び出されていると言えます。_get_current_object