使用threading.Lock作为上下文管理器

编程入门 行业动态 更新时间:2024-10-23 15:20:38
本文介绍了使用threading.Lock作为上下文管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在线程模块:

此模块提供的具有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作为上下文管理器

本文发布于:2023-11-23 10:07:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621053.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   管理器   threading   Lock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!