builtins.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:RealtimePythonChat 作者: quangtqag 项目源码 文件源码
def __import__(*args, **kwargs):
    """
    __import__(name, globals=None, locals=None, fromlist=(), level=0) -> object

    Normally python protects imports against concurrency by doing some locking
    at the C level (at least, it does that in CPython).  This function just
    wraps the normal __import__ functionality in a recursive lock, ensuring that
    we're protected against greenlet import concurrency as well.
    """
    if len(args) > 0 and not issubclass(type(args[0]), _allowed_module_name_types):
        # if a builtin has been acquired as a bound instance method,
        # python knows not to pass 'self' when the method is called.
        # No such protection exists for monkey-patched builtins,
        # however, so this is necessary.
        args = args[1:]

    if not __lock_imports:
        return _import(*args, **kwargs)

    module_lock = __module_lock(args[0]) # Get a lock for the module name
    imp.acquire_lock()
    try:
        module_lock.acquire()
        try:
            result = _import(*args, **kwargs)
        finally:
            module_lock.release()
    finally:
        imp.release_lock()
    return result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号