Reputation: 705
I'm working with testing a neural network model on hardware and have the model code in Python and hardware code in C++.
From the C++ code, I'm calling
system("python file.py")
to run my Python script containing the model, along with the pre-processing and post-processing steps. There's a lot of sequential data I want the model to be able to process, but I want it to be real-time (so as fast as possible). As a result, I'd like to split up my python file into 2 pieces - one with the pre-processing steps (such as loading the model/files) and the other with the actual inference step (which will be run for each piece of data for each second).
But, how do I do that in my C++ code?
If I call:
system("python preprocessing.py")
and then
system("python inference.py")
they won't be connected right? Inference.py will have no clue what happened in the preprocessing step. Basically, my question is - how can I maintain state between the 2 python script calls that I'm making from C++?
Is it possible that I call the preprocessing step and then somehow "wait" for further input and then perform inference on each sequential input?
Upvotes: 2
Views: 792
Reputation: 292
I am not a python expert, but I would approach this problem differently - I would keep the python script as 1 file and use Interprocess communication between the C++ and Python programs to transfer signals (i.e. - new data is available).
Essentially, the python script will do the initial setup/preprocessing and then wait for a signal from the C++ app that the data is ready for processing (i.e. busy wait or sleep on an event).
The simplest way to do it is to leave a message in a common file, but I am sure there are several other methods that windows provides (such as interprocess mutexs or message queues).
Hope this gives you a plan...
Upvotes: 1
Reputation: 5757
I would suggest to have a single python script and have different functions within the script, say preprocess
and infer
. Then use the embed script approach to call the functions within the script. Unless you have to pass a lot of variables to the python functions it is pretty straight forward.
The example program from the documentation here works without much modification. Look at section 1.3
Basically you need to call the Py_Initialize
function then load the module, get the function pointer corresponding to the functions in the script and then call them.
Hope that helps.
Cheers!
Upvotes: 1