Java-Python共享内存通信 [英] Java - Python shared memory communication

查看:404
本文介绍了Java-Python共享内存通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Java程序和一个Python程序,需要以乒乓方式将它们组合在一起,每次交换一个长度为100,000的整数数组,并花费〜0.1-1秒来完成工作:

We have one program in Java and one in Python, and need to get them taking together in a ping-pong manner, each time exchanging an integer array of length 100,000, and taking ~ 0.1 - 1 second to do their work:

  1. Java 进行了一些工作,并将长度为100,000的int数组发射到...
  2. Python ,它可以完成一些工作并向…发射一个新的长度为100,000的新数组.
  3. Java ,可以完成一些工作...等等
  1. Java does some work and fires an int array of length 100,000 over to ...
  2. Python, which does some work and fires a new array of length 100,000 back to ...
  3. Java, which does some work ... etc

请注意

  • 每个程序都需要等待另一个程序完成.
  • 它们将在同一台Linux计算机上运行.
  • 我们将进行蒙特卡洛模拟,因此速度非常重要.

我更熟悉Java,并且了解共享内存支持的文件方法可能是最快的. 似乎与Java端有关,但是我如何让每个程序等待/阻止另一个程序完成其工作并在另一个程序开始读取之前更新共享内存呢?我听说过一种叫信号量"的东西,但无法弄清楚.

I am more familiar with Java, and understand that a shared memory backed file approach is likely to be the fastest. This seems relevant for the Java side, but how would I get each program to wait/block for the other to complete its work and update the shared memory before the other starts reading? I've heard of something called 'semaphore', but can't figure it out.

这些是我的后备主意,但也许更好一些?

These are my fallback ideas, but perhaps they are better?

  • Unix Domain Sockets with jnr-unixsocket
  • Sockets with Speedus

推荐答案

您可以尝试使用共享内存的支持在python和java之间使用 numpy ndarrays 和java 直接缓冲区.这样一来,您就可以共享数据而无需进行任何复制,而复制应该可以提供最佳性能.

You could try combining java and python in the same process using jep. The latest release added support for sharing memory between python and java using numpy ndarrays and java direct buffers. This would let you share the data without any copying which should give the best performance possible.

这篇关于Java-Python共享内存通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆