简体   繁体   中英

Simplest way to communicate between Python and C# using IPC?

I have some C# code that needs to call a Python script several thousand times, each time passing a string, and then expecting a float back. The python script can be run using ANY version of Python, so I cannot use Iron python. It's been recommended that I use IPC named pipes. I have no experience with this, and am having trouble figuring out how to do this between C# and Python. Is this a simple process, or am I looking at a decent amount of work? Is this the best way to solve my problem?

Use zeromq.

  • allows very light messaging without a need for broker
  • is available for many platforms
  • can communicate over TCP socket, unix socket or inside a process

Here is my answer using zeromq https://stackoverflow.com/a/23227631/346478

It serves for messaging between different Python programs, but exactly the same method of communication can be used between other platforms. Just keep the messages passed around interopable - both sides must understand the content properly. You can play with binary data, but very often json representation work quick and easy too. But there are many serialization frameworks like result buffers and others, which might help you to encode result.

Based on the what you have said, you can connect to the python process and catch standard output text. Easy, fast and reliable!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM