我在Mercurial中有一个Python应用程序。在应用程序中,我发现需要显示当前正在运行的提交。到目前为止,我发现的最佳解决方案是使用hglib
。我有一个看起来像这样的模块:
def _get_version():
import hglib
repo = hglib.open()
[p] = repo.parents()
return p[1]
version = _get_version()
这使用hglib
查找已使用的版本并将结果存储在变量中,我可以在服务保持运行的整个时间内使用该变量。
我现在的问题是这会让hg
子进程运行,这对我来说没用,因为只要这个模块完成初始化,我就不需要使用hglib
了。
一旦我对存储库实例的引用超出范围,我原本期望在垃圾回收期间关闭子进程。但显然这不是它的工作方式。
在阅读hglib
文档时,我没有找到有关如何关闭子进程的任何文档。
完成hg
子进程关闭后,首选方法是什么?
答案 0 :(得分:2)
您需要将repo类似于文件。您在完成后调用repo.close()或在with:
中使用它def _get_version():
import hglib
with hglib.open() as repo:
[p] = repo.parents()
return p[1]
version = _get_version()