0

I need to send a string from C# to a Python script, have the script do some magic, then return a string. This needs to happen ~million times. Right now i'm doing the communication just using STDIN/STDOUT, but it's very slow. I did a quick testing comparing IronPython vs this approach and it was orders of magnitude faster.

The only way I can think of to fix this is to instead of sending over one string at a time to Python, write all the strings to a file, then have Python read the entire file in and write back values to a new file. While I assume this would work, it would require a decent amount of changes to existing code to support sending over all the values at once.

Is there some faster form of communication I can use between C# and Python? One annoying catch here is that this needs to support any version of Python (2.7, 3.0, IronPython etc etc).

user3715648
  • 1,498
  • 3
  • 16
  • 25
  • This question might have been addressed already. Please check out this posting: http://stackoverflow.com/questions/23374854/simplest-way-to-communicate-between-python-and-c-sharp-using-ipc – Trajan Unger Jun 23 '14 at 21:56
  • 1
    I think there are two separate issues here: IPC and batching. It's important to note that using IronPython is not IPC; it's literally just a function call in the same process. IPC is inherently slow, especially if you need to do it 1m times. To decrease the overhead of IPC, you can either not use IPC (e.g. IronPython; or use PInvoke to call an embedded CPython directly), or to batch your calls (e.g. your file idea). Of the two, batching is much more effective IMO; it doesn't matter what your IPC mechanism is; as long as you have to do it 1m times it adds up to significant overhead. – univerio Jun 23 '14 at 22:18

0 Answers0