I need a way for c# to communicate with an arbitrary version of Python. The user passes in a path to Python, so they can choose any version they want. I need to be able to pass a string to Python, and get back a float. This needs to happen several thousand times. Right now i'm just using standard in/out, but it seems to be very slow. Is there any faster way to do this?
-
Have you checked out IronPython (http://ironpython.net/)? – sous2817 Apr 25 '14 at 18:19
-
http://ironpython.net/ – selbie Apr 25 '14 at 18:19
-
Yeah. The issue is that the user can pass in a path to Python, and can choose whatever they want. – user3490130 Apr 25 '14 at 18:20
-
What about using a socket? – juan.facorro Apr 25 '14 at 18:22
-
What about gRPC? It's officially supported by Microsoft/.Net. – Hieu Jun 02 '20 at 06:20
2 Answers
Typically, the "best" way to handle this type of scenario is to use IronPython instead of having the user supply their own Python runtime.
You can also use Python for .NET, but this again will tie you to a specific version of Python.
If you control what's called in Python, but need arbitrary versions of python, you may be able to use a common messaging service. One option would be to setup an IPC channel via pipes, which are supported in Python and .NET. This would provide fast C#->Python->C# communication while keeping the process separate.
If you need arbitrary versions and installations of python, than executing and fetching information via the shell process may be the only option.

- 479
- 4
- 9

- 554,122
- 78
- 1,158
- 1,373
Very fast is using zmq
, libraries are available for both, C# and Python, can talk across network, in some situations is faster then plain TCP.
E.g. iPyhton is using it for internal inter-component communication.
My StackOverflow answer shows Lock server implemented over zmq which could be used to serve C# too.

- 1
- 1

- 42,725
- 12
- 101
- 98