Lock.__enter__() 和 Condition.__enter__() 的文档

Posted

技术标签:

【中文标题】Lock.__enter__() 和 Condition.__enter__() 的文档【英文标题】:Documentation for Lock.__enter__() and Condition.__enter__() 【发布时间】:2014-07-13 12:19:32 【问题描述】:

我正在尝试查找 Python 2.7 中记录了线程对象的 __enter____exit__ 方法的位置。该主题上的main documentation page 没有任何类型。这有记录吗?

也许,他们应该做什么如此明显,以至于我在问一个荒谬的问题?这对我来说当然不是很明显。在我的理解中,__enter__ 应该获取锁,__exit__ 应该释放它,但是锁获取带有可选参数(阻塞/非阻塞),所以它有点模棱两可,至少对我自己来说。

【问题讨论】:

你看到this了吗? @BrenBarn 不知道我是怎么错过这个.....漫长的一天,谢谢。 【参考方案1】:

正如BrenBarn in the first comment 所指出的,文档确实提到了如何使用带有with 语句here 的锁,这是__enter____exit__ (see `with` statment context managers) 的隐式文档。

注意:BrenBarn 链接的文档是 Python 2.7 的文档,最新的(在撰写 Python 3.10 时)可以在 here 找到。

【讨论】:

以上是关于Lock.__enter__() 和 Condition.__enter__() 的文档的主要内容,如果未能解决你的问题,请参考以下文章

python with as

解释 Python 的 '__enter__' 和 '__exit__'

实现文件上下文管理(\_\_enter\_\_和\_\_exit\_\_)

__enter__和__exit__的用法

python的with和__enter__ __exit__

上下文管理器__enter__和__exit__