瀏覽代碼

Make lazy importing code thread-safe.

tags/v0.2
Ben Kurtovic 10 年之前
父節點
當前提交
96631e25f4
共有 2 個文件被更改,包括 15 次插入14 次删除
  1. +2
    -1
      CHANGELOG
  2. +13
    -13
      earwigbot/lazy.py

+ 2
- 1
CHANGELOG 查看文件

@@ -4,7 +4,8 @@ v0.2 (unreleased):
- Fixed API behavior when blocked, when using AssertEdit, and under other
circumstances.
- Added copyvio detector functionality: specifying a max time for checks;
improved exclusion support. Fixed assorted bugs.
improved exclusion support. URL loading and parsing is parallelized to speed
up check times. Fixed assorted bugs.
- Added support for Wikimedia Labs when creating a config file.
- Added and improved lazy importing for various dependencies.
- Fixed a bug in job scheduling.


+ 13
- 13
earwigbot/lazy.py 查看文件

@@ -29,22 +29,21 @@ until they are used (i.e. their attributes are read or modified).

from imp import acquire_lock, release_lock
import sys
from threading import RLock
from types import ModuleType

__all__ = ["LazyImporter"]

def _getattribute(self, attr):
_load(self)
return self.__getattribute__(attr)
_real_get = ModuleType.__getattribute__

def _setattr(self, attr, value):
_load(self)
self.__setattr__(attr, value)
def _load(self):
type(self).__getattribute__ = ModuleType.__getattribute__
type(self).__setattr__ = ModuleType.__setattr__
reload(self)
def _mock_get(self, attr):
with _real_get(self, "_lock"):
if _real_get(self, "_unloaded"):
type(self)._unloaded = False
reload(self)
type(self).__getattribute__ = _real_get
del type(self)._lock
return _real_get(self, attr)


class _LazyModule(type):
@@ -54,8 +53,9 @@ class _LazyModule(type):
if name not in sys.modules:
attributes = {
"__name__": name,
"__getattribute__": _getattribute,
"__setattr__": _setattr
"__getattribute__": _mock_get,
"_unloaded": True,
"_lock": RLock()
}
parents = (ModuleType,)
klass = type.__new__(cls, "module", parents, attributes)


Loading…
取消
儲存