Interoperability – How to Have Two Programs in Different Languages Interact

cpython

I don't know if this is too broad or not, but I am a youngish programmer still in college, its my Junior year. I feel like I have a pretty good grasps for different languages and have a pretty good base. But I am stumbling to think how if for example, I am trying to create a program and say I wrote one part in python just because its easy, and does the job, but this program would need to get output from another program that I wrote in C and I am using C because of its speed. I am not sure how to have the two different programs and languages interact with each other to create an overall one total program. I am thinking of like sure you can write to a file, but then what if python and C programs both accessing a file I would need to think of locks.

Most times I have done this was with importing files into a program, but in that case they are the same language so that is easy I just use the import function, but with two languages/programs interacting to create one cohesive output I am having trouble.

I was thinking about this question because I was thinking of diving into creating some basic web applications just to learn but I have no idea how to have say javascript file interactive with something that I wrote in python or vice-versa.

I feel like I am missing something really easy here and just not understanding. Sorry if this question is too broad but I couldn't really find a clear answer online, I was trying to look through an opensource webapp, but couldn't really grasp an answer from it, again pardon me if the question seems dumb I thought this be a good place to ask I love reading on stackexchange.

Thank you for any reply.

Best Answer

Code written in different languages can interact in a number of ways.

At the source level, cross-compilation from one language into the other can be done for some combinations of languages (for example, Google's GWT includes a java-to-javascript compiler; the Glasgow Haskell compiler can compile to C; early versions of C++ compiled to C). Most of the time, however this is not really feasible.

Languages that share a virtual platform, such as the JVM or the .NET runtime, can usually interact through mechanisms exposed by the platform - for example all JVM languages can access Java libraries and use them to communicate among each other, and they can call methods and use classes created in any other JVM language.

Many programming languages, including Python, offer a mechanism to interface with native libraries, typically written in C. Using such a mechanism, it is possible to call native functions from another, more high-level, language. Popular libraries often have bindings readily available. This technique is usually referred to as a "Foreign Function Interface". The Python-into-C interface is the CFFI.

Another option is to build two completely separate programs and have them interact at runtime. There are various mechanisms to achieve this; the easiest is through a pipe (look into the subprocess module for python): basically, one program calls the other, sending input to its stdin and reading the result back from its stdout. This makes one program a subprocess of the other; if you need both to be long-lived and started independently, data can be passed back and forth through named pipes, (local) network sockets, shared files, and (depending on the platform) other means. Which one is best depends.

Related Topic