How do I stop hg child process when using hglib

151 views Asked by At

I have a Python application in Mercurial. In the application I found the need to display from which commit it is currently running. The best solution I have found so far is to make use of hglib. I have a module which looks like this:

def _get_version():
    import hglib
    repo = hglib.open()
    [p] = repo.parents()
    return p[1]

version = _get_version()

This uses hglib to find the used version and stores the result in a variable, which I can use for the entire time the service remains running.

My problem now is that this leaves a hg child process running, which is useless to me, since as soon as this module is done initializing, I don't need to use hglib anymore.

I would have expected the child process to be shut down during garbage collection once my reference to the repository instance goes out of scope. But apparently that is not how it works.

When reading the hglib documentation I didn't find any documentation on how to get the child process shut down.

What is the preferred method to get the hg child process shut down once I am done with it?

1

There are 1 answers

0
CrazyCasta On BEST ANSWER

You need to treat repo sort of like a file. Either call repo.close() when you're done or use it inside a with:

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

version = _get_version()