1

I have a c++ code that performs certain task and gives out 4 integers in a loop. The communication part related to task is written in python. What is the best way to grab the integers in a python script such that i can use it with the communication module ? Currently I'm thinking of priting the c++ integers to a file and read the same file with python script, which is very inefficient. How can I implement a shared memory ?

./a.out Output: a,b,c,d
comm.py Input: a,b,c,d
Electrix
  • 113
  • 1
  • 9
  • https://docs.python.org/2/library/subprocess.html – Henri Menke Jul 20 '18 at 10:09
  • 1
    This is answered here: https://stackoverflow.com/questions/4537259/python-how-to-pipe-the-output-using-popen You open a pipe to a running process that python starts using popen. Then you read through the pipe. – Owl Jul 20 '18 at 10:13
  • is it needed to print the output ? I'm looking for a way where i dont have to print the output but share the memory with python – Electrix Jul 20 '18 at 10:18
  • 1
    Seems like it's not integers you are trying to acquire. It's the standard output (content) or whatever is piped to it. And it's no C++ code either. What gets executed on the machine is neither C++ nor Python. – Ron Jul 20 '18 at 10:20
  • You can just get the data directly from popen and use it, it doesn't have to print out data. However, it's possible that if the application you open writes data to stderr, that may not be captured by the pipe - you need to check that. In that situation you may get stderr data printed out. You can get around that issue by either disregarding the stderr by writing it to /dev/null, or by redirected stderr to stdout. – Owl Jul 20 '18 at 10:21
  • 1
    If there is std::cout/cerr output you do not want to deal with, you could use a named pipe as well. – Aconcagua Jul 20 '18 at 10:23
  • 1
    As far as I am aware, sharing memory across processes did not make it into the standard yet (please correct me anyone if I'm wrong...). So you'd rely on either OS API directly or use some third party library (I think boost offers such features, for instance). Python? Not sure if there is support for at all, in worst case, you'd write a python extension for. After all, using pipes (in either variant) probably makes life easier for you. – Aconcagua Jul 20 '18 at 10:27
  • @Owl if i'm not printing, how will the program know which variables I'm trying to grab? Can you paste some rough sample code for me to better understand. – Electrix Jul 20 '18 at 10:31
  • @Ron I wanted to convey I can make necessary changes to the c++ code if needed for the job. – Electrix Jul 20 '18 at 10:31
  • 1
    @Electrix All you need (in python) you find in [popen documentation](https://docs.python.org/3/library/subprocess.html#subprocess.Popen). What you read from is what you put to std::cout in your c++ programme... – Aconcagua Jul 20 '18 at 10:35
  • 1
    @Electrix, I am sorry, I misunderstood, yes the C++ program does need to print out data for the data to be captured. Sorry i thought you were talking about the Python script. – Owl Jul 20 '18 at 10:35
  • @Aconcagua is there a way I can do the same without putting it to std::out? I can make necessary code changes to the c++ side if needed. – Electrix Jul 20 '18 at 10:37
  • 1
    @Electrix you can use a named pipe instead as I commented already. Consult your favourite search engine with "c++ named pipe" and "python named pipe" and you should get quite a lot of suitable links. – Aconcagua Jul 20 '18 at 11:20

0 Answers0