Fast IPC/Socket communication in Java/Python

前端 未结 2 579
灰色年华
灰色年华 2021-02-02 03:14

Two processes (Java and Python) need to communicate in my application. I noticed that the socket communication takes 93% of the run time. Why is communication so slow? Should I

2条回答
  •  感情败类
    2021-02-02 04:08

    Some thoughts

    • The server is in Java, the client is written in Python.

    An odd combination, but is there any reason one cannot call the other sending via stdin, stdout?

    • Socket IPC is implemented below: it takes 50 cycles sending 200 bytes ! This has got to be too high. If I send 2 bytes in 5000 cycles, it takes a lot less time.

    Any call to the OS is going to be relatively slow (latency wise). Using shared memory can by pass the kernel. If throughput is you issue, I have found you can reach 1-2 GB/s using sockets if latency isn't such an issue for you.

    • Both processes run on one Linux machine.

    Making shared memory ideal.

    • In the real application about 10 calls to client's iFid.write() are made each cycle.

    Not sure why this is the case. Why not build a single structure/buffer and write it once. I would use a direct buffer is NIO to minimise latency. Using character translation is pretty expensive, esp if you only need ASCII.

    • This is done on a Linux system.

    Should be easy to optimise.

    I use shared memory via memory mapped files. This is because I need to record every message for auditing purposes. I get an average latency of around 180 ns round trip sustained for millions of messages, and about 490 ns in a real application.

    One advantage of this approach is that if there are short delays, the reader can catch up very quickly with the writer. It also support re-start and replication easily.

    This is only implemented in Java, but the principle is simple enough and I am sure it would work in python as well.

    https://github.com/peter-lawrey/Java-Chronicle

提交回复
热议问题