在线程模块:
此模块提供的具有acquire()和release()方法的所有对象都可以用作 with 语句.进入该块时将调用acquire()方法,退出该块时将调用release().
All of the objects provided by this module that have acquire() and release() methods can be used as context managers for a with statement. The acquire() method will be called when the block is entered, and release() 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块的开头)和 __exit__() ,在最后被调用.
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作为上下文管理器
发布评论