使用threading.Lock作为上下文管理器 [英] Using threading.Lock as context manager

查看:72
本文介绍了使用threading.Lock作为上下文管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程模块:

此模块提供的具有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块的开头)和

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆