Java 和 PHP 共享内存?
有人可以向我提供 PHP 进程写入内存和 Java 进程从共享内存读取的链接或片段吗?
感谢您的精彩回答。
编辑的问题: 假设我像这样在 php 中创建一个共享内存
<?php
$shm_key = ftok(__FILE__, 't');
$shm_id = shmop_open($shm_key, "c", 0644, 100);
$shm_bytes_written = shmop_write($shm_id, $my_string, 0);
?>
现在有一些方法可以传递 $shm_id
的值,然后在 java 中读取它。
Can someone provide me links or snippets where a PHP process writes to memory and a Java process reads from the shared memory?
Thanks for the wonderful answers.
Edited question :
Suppose i create a shared memory in php like this
<?php
$shm_key = ftok(__FILE__, 't');
$shm_id = shmop_open($shm_key, "c", 0644, 100);
$shm_bytes_written = shmop_write($shm_id, $my_string, 0);
?>
Now is there some method by which i can pass the value of $shm_id
and then read from it in java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不需要 Java 和 PHP 之间的同步交互 - 我会使用 memcached, membase 或其他类型的内存密钥存储。
对于大量数据流,另一种方法是使用 Unix 命名管道 (FIFO)。这是IPC(进程间通信)中常见的方式。
首先使用 mkfifo 命令将管道创建为普通文件。添加一些合理的访问权限。在PHP中以
r+
模式像普通文件一样打开管道并写入,最后关闭。在 Java 端,您将其作为普通文件打开,并通过 FileInputStream 连续读取,并使用阻塞读取/读取行或非阻塞 NIO。与 SHM 相比,您不必使用 JNI、共享内存同步、环形缓冲区实现、锁定和内存泄漏。您可以以最低的开发成本获得简单的读/写和 FIFO 队列。
您将其作为普通文件删除。不要使用随机访问或
seek
,因为它是没有历史记录的真实流。If you don't need synchronized interaction between Java and PHP - I would use memcached, membase or some other type of memory key store.
Another way, for huge amount of data stream, is using Unix named pipe (FIFO). It is common way in IPC (Inter Process Communication).
First create the pipe as normal file using
mkfifo
command. Add some reasonable access rights. In PHP open the pipe withr+
mode as normal file and write, finally close. On Java side you keep it open as normal file and read continuously byFileInputStream
with blockingread
/readline
or nonblocking NIO.In comparison to SHM, you don't have to play with JNI, shared memory synchronization, ring buffer implementations, locking and memory leaks. You get simple read/write and FIFO queue at lowest development cost.
You delete it as normal file. Don't use random access or
seek
as it is real stream without history.为什么不使用一些消息队列呢?您不能从字面上写入 JVM 内存或与其他进程共享它。
为了在其他人之间进行通信,您可以使用消息队列技术。您可以运行消息队列,并且 PHP 可以轻松传输数据。 java应用程序可以读取队列,获取数据并进行相应的处理。
Why don't you use some message queues? You cannot literally write into memory of a JVM or share it with other processes.
In order to communicate between others you can make use of a Message Queue technology. You can have message queue running and PHP could easily transfer the data. The java application could read the queue, get the data and process accordingly.
为了扩展 Abdel 的答案,我推荐 RabbitMQ,它具有 Java 和 PHP 客户端。
To expand on Abdel's answer, I'd recommend RabbitMQ, which has Java and PHP clients.