interprocess C# python real time

放肆的年华 提交于 2019-12-18 06:41:07

问题


I'm working on a project where I'll have one application in C# and another one in Python. The C# application will continuously analyse stream of data and raise a flag each time something interesting is detected. So each time there will be an event, my Python application will have to read it and continues with it own process, while other flags will continue being sent. As you understand the C# app won't wait for the Python one to finish its computation before sending another flag.

So I was wondering if it was possible to create a sub/pub (C# being the Publisher, and Python the Subscriber), if yes how can I do it, and do you think it's a good idea? I'm pretty new in this field, so could you tell me if there are other possibilities?

Thx for your help.


回答1:


Simplest way is PIPE communication. another simple way that not suggested is SOCKET programming. Pipes and Named pipes are good solution to communicate between different processes (over different programming languages). SOCKET programming is like this but may need more Access Level and may be less security.

other type of IPCs seems be unusable.

see for more info:

  • C# - Pipes

  • python - Pipes




回答2:


Redis pub/sub is awesome... or ZeroMQ.




回答3:


You may use our aktos-dcs and aktos-dcs-cs libraries. I have successfully used these libraries in production in order to make an RFID Reader (from Impinj) communicate with (actually, integrated in) our telemetry system. The RFID reader has a C# API and we are heavily using Python in our telemetry system.

The simplest test case is a pinger-ponger application, and here is how it looks like with these libraries:

pinger.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using aktos_dcs_cs;

namespace pinger
{
    class Pinger : Actor
    {
        public void handle_PingMessage(Dictionary<string, object> msg)
        {
            Console.WriteLine("Pinger handled PingMessage: {0} ", msg["text"]);

            string msg_ser = @"
                    {""PongMessage"": 
                        {""text"": ""this is proper message from csharp implementation""}
                    }
                ";
            System.Threading.Thread.Sleep(2000);
            send(msg_ser);
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Pinger x = new Pinger(); 
            Actor.wait_all(); 
        }
    }
}

ponger.py:

from aktos_dcs import *

class Pinger(Actor):
    def handle_PingMessage(self, msg_raw):
        msg = get_msg_body(msg_raw)
        print "Pinger got ping message: ", msg['text'], (time.time() - msg_raw['timestamp'])
        sleep(2)
        self.send({'PongMessage': {'text': "Hello ponger, this is pinger 1!"}})

if __name__ == "__main__":
    ProxyActor()
    pinger = Pinger()
    pinger.send({'PongMessage': {'text': "Hello ponger, this is STARTUP MESSAGE!"}})

    wait_all()


来源:https://stackoverflow.com/questions/11371448/interprocess-c-sharp-python-real-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!