使用threading.Lock作为上下文管理器 [英] Using threading.Lock as context manager
问题描述
在线程模块:
All of the objects provided by this module that have
acquire()
andrelease()
methods can be used as context managers for awith
statement. Theacquire()
method will be called when the block is entered, andrelease()
will be called when the block is exited.
我想知道是在阻止还是非阻止模式下调用它?
I was wondering if it is called in blocking or non-blocking mode?
推荐答案
通过查看 CPython来源,它似乎是使用默认参数调用的,这意味着处于阻止模式.
From looking at the CPython source, it appears that it's called with default arguments, which means in blocking mode.
您要特别查看的方法是 __enter__()
(在with
块的开头)和
The methods you want to look at in particular are __enter__()
, which is called at the beginning of the with
block, and __exit__()
, which is called at the end.
这篇关于使用threading.Lock作为上下文管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!