Embedding Python code in C++

419 views
Skip to first unread message

Ahmed Nasrallah

unread,
Feb 5, 2021, 6:09:50 PM2/5/21
to OMNeT++ Users
Hello,

I have a slight problem where I'm trying to embed python code using the python/c API. I added the library paths and can now include python.h but it fails when linking. Does anyone know how to embed python code in the omnet++ ecplise IDE? I'm trying to use a machine learning model using Keras/TensorFlow that I trained in python and want to load it on my omnet++ project. If someone can point me in the correct direction I'd appreciate it.

Thanks,
Ahmed

My Custom Novels

unread,
Feb 14, 2021, 9:30:32 AM2/14/21
to OMNeT++ Users
Hi, one thing you can do is if you need a specific output from the python code, you can define the following function in the C++ code:
string exec(const char* cmd){
    std::array<char, 128> buffer;
    std::string result;
    std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
    if (!pipe) {
        throw std::runtime_error("popen() failed!");
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
    return result;
}
You can make the python code such that it prints the needed output to stdout. Then to retrieve that output from C++ all you need is
string s=exec("C:/Users/User/AppData/Local/Programs/Python/Python36/python.exe C:/Users/User/Documents/folder/main.py");
Where 'C:/Users/User/Documents/folder/main.py' basically means whatever your python program is.
 "s" is a string containing the output of the python code. This can always be converted to double or other data types as needed.'

If you want to supply input to the python code from OMNET++, one quick solution is to write to a file which is subsequently read by the running python code. Otherwise you will have to establish a server connection to python which may be more difficult. 

serap bakioglu

unread,
Mar 23, 2022, 11:47:41 AM3/23/22
to OMNeT++ Users
Dear Ahmed,
I am working on something you want to,too.
But unfortunately; I could not.
Did you get any progress about it?

6 Şubat 2021 Cumartesi tarihinde saat 00:09:50 UTC+1 itibarıyla Ahmed Nasrallah şunları yazdı:

Ahmed Nasrallah

unread,
Mar 23, 2022, 11:52:37 AM3/23/22
to omn...@googlegroups.com
I did. I used socket programming to send and receive the data for the machine learning part in Keras. I had a python server that houses the machine learning pre trained model while the omnet is the client that queries the server by sending the data that I construct at runtime. 

--
You received this message because you are subscribed to the Google Groups "OMNeT++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to omnetpp+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/omnetpp/23663510-9d32-4e46-b5d8-d42b3900133dn%40googlegroups.com.
--
Sent by my iPhone

serap bakioglu

unread,
Mar 24, 2022, 11:58:58 AM3/24/22
to OMNeT++ Users
And also I have two document that found in these google group.
But while  importing these into a OMNeT project, I couldn't follow any steps.
Could you give me a hand, please?

23 Mart 2022 Çarşamba tarihinde saat 16:52:37 UTC+1 itibarıyla Ahmed Nasrallah şunları yazdı:
cppcode.txt
pythonside.py
Reply all
Reply to author
Forward
0 new messages