1

I am writing a python script that generates a list of integers and is supposed to send this list to a concurrently running C++ program that is constantly awaiting input. What is a quick and painless way to design the link between these two languages?

I have considered possibilities such as:

  • writing the python result to a file, while listening for file updates in C++ and loading it as soon as new data is available.
  • using python pexpect package and including a command line interface in the C++ program, so that it receives the data through cin.

Both above possibilities seem a bit too hacky to me. Are there better options, which would not require to become an expert in C++ library coding for python?

Kagaratsch
  • 963
  • 8
  • 18
  • 1
    Maybe this could help you: https://stackoverflow.com/a/18860898/5369706. Thinking you can write to standard output and input in Python and catch the stream in C++. – Jason R Stevens CFA Jul 12 '20 at 04:35

1 Answers1

1

You could set up a Socket and an OutputStream in the python app to send the data, and another Socket and an InputStream in the C++ program to receive the data.

Tiago Orl
  • 36
  • 4
  • I was under the impression that sockets bind to IPs and ports and are designed for internet communication. Is there a way to set up sockets purely locally? Or are you suggesting to send the data through the internet? – Kagaratsch Jul 12 '20 at 00:42
  • 1
    Yes there is, on your python app, when creating a new socket you can just use a "not reserved" port like "7778" and use localhost(which is usually 127.0.0.1) to connect to the same machine, for example, create a new socket with 'new Socket("localhost", 7778)'. And in your c++ you just need to set up a new Socket on the same port (7778, for example). – Tiago Orl Jul 12 '20 at 01:01
  • This is great, I'll certainly give this a try! Accepting this answer, at least until something even better comes around. – Kagaratsch Jul 12 '20 at 01:04