Question

In case both options available: to call a command line tool with subprocess (say, hg) or to make use of native python API (say, mercurial API), is there a case where it's more favorable to use the former?

Était-ce utile?

La solution

If you want to execute some third party native code which you know is not stable and may crash with a segvault then it is better to execute it as a subprocess - you will be able to safely handle the possible crashes from your Python process.

Also, if you want to call several times some code which is known to leak memory, leave open files or other resources, from a long running Python process, then again it may be wise to run it as a subprocess. In this case the leaking memory or other resources will be reclaimed by the operating system for you each time the subprocess exits, and not accumulate.

Autres conseils

The only way that i see myself using subprocess instead of a native python api is if some option of the program is not provided in the api.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top