使用 hglib 时如何停止 hg 子进程

How do I stop hg child process when using hglib

我在 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 子进程后关闭它的首选方法是什么?

您需要像对待文件一样对待 repo。完成后调用 repo.close() 或在 with:

中使用它
def _get_version():
    import hglib
    with hglib.open() as repo:
        [p] = repo.parents()
    return p[1]

version = _get_version()