2

I have an executable (face recognition) written by one of our programmers who's no longer with us. It takes a commandline argument and prints the result to STDOUT like this.

I need to be able to use this in Python but performance is important so I'm not sure if I should just use subprocess. I need to capture the output of the program so is it a better idea to modify the code to make it a C++ library instead and write a wrapper for it in Python? How would that impact performance compared to using subprocess?

I have looked into the Python docs for running C code and found this but the answer here is different. The second method does not even use Python.h so I'm a little confused. What's the best way to do what I need when performance is important?

10
  • 1
    Why the downvote and why is the C++ tag removed when that's exactly what I'm asking? Commented Jan 30, 2014 at 2:09
  • Executable or library imported function, decide! Removed c++ tag didn't downvote so far ...) Commented Jan 30, 2014 at 2:09
  • 2
    @πάνταῥεῖ: Yes, Crypto needs to decide, but that's no reason for a downvote. I don't think your edit or re-tag was warranted in this case. Commented Jan 30, 2014 at 2:11
  • @DanielPryden I didn't downvote, and your edit, (re-)adding c++ is correct, sorry ... Commented Jan 30, 2014 at 2:14
  • 1
    @Crypto: If your question is "How can I call some arbitrary C++ code from Python", then it's an exact duplicate of the question you linked to, which means it should be closed as a duplicate. If your question is "How can I execute a C++ program from Python?" then the answer is probably to use the subprocess module. πάντα ῥεῖ is right that it doesn't really make sense to ask both at once -- perhaps what you're really asking is "when should I link in a C++ library instead of using a subprocess"? Commented Jan 30, 2014 at 2:14

1 Answer 1

2

At first I have to tell you I think subprocess method and running external application and reading output from STD output would be slow and unreliable. I won't recommend that method.

Instead, here is some suggestions:

  • You can create a Python extension in C or C++, you can find a lot of resources online about it. So you can convert your current code to Python extension format and just use Python extension skeleton template, integrate your functions and code in it and you'll have a python extension, simply you'll be able to call it's functions from Python code. See: http://docs.python.org/2/extending/index.html

  • Convert your current C++ code directly into a DLL and exports functions of your code you want to call from python if you are in Windows or simply convert it to a shared library if you are using Linux. It will be compiled to .so . Then you can call functions as an external library in your Python code. Example with explanation: http://www.linuxforu.com/2010/05/extending-python-via-shared-libraries/

I've never tried them, but it seems these projects can help you with what you want:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.