fod*_*don 7 python java sockets ipc
两个进程(Java和Python)需要在我的应用程序中进行通信.我注意到套接字通信需要93%的运行时间.为什么沟通这么慢?我应该寻找套接字通信的替代方案,还是可以加快速度?
更新:我发现了一个简单的修复.似乎缓冲输出流由于某种未知原因而没有真正缓冲.所以,我现在将所有数据放入客户端/服务器进程的字符串缓冲区中.我在flush方法中将它写入套接字.
我仍然对使用共享内存在进程之间快速交换数据的示例感兴趣.
一些其他信息:
这是服务器端:
public class FastIPC{
public PrintWriter out;
BufferedReader in;
Socket socket = null;
ServerSocket serverSocket = null;
public FastIPC(int port) throws Exception{
serverSocket = new ServerSocket(port);
socket = serverSocket.accept();
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void send(String msg){
out.println(msg); // send price update to socket
}
public void flush(){
out.flush();
}
public String recv() throws Exception{
return in.readLine();
}
public static void main(String[] args){
int port = 32000;
try{
FastIPC fip = new FastIPC(port);
long start = new Date().getTime();
System.out.println("Connected.");
for (int i=0; i<50; i++){
for(int j=0; j<100; j++)
fip.send("+");
fip.send(".");
fip.flush();
String msg = fip.recv();
}
long stop = new Date().getTime();
System.out.println((double)(stop - start)/1000.);
}catch(Exception e){
System.exit(1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
客户端是:
import sys
import socket
class IPC(object):
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect(("localhost", 32000))
self.fid = self.s.makefile() # file wrapper to read lines
self.listenLoop() # wait listening for updates from server
def listenLoop(self):
fid = self.fid
print "connected"
while True:
while True:
line = fid.readline()
if line[0]=='.':
break
fid.write('.\n')
fid.flush()
if __name__ == '__main__':
st = IPC()
Run Code Online (Sandbox Code Playgroud)
sni*_*im2 12
你有很多选择.由于您使用的是Linux,因此可以使用UNIX域套接字.或者,您可以将数据序列化为ASCII或JSon或其他格式,并通过管道,SHM(共享内存段),消息队列,DBUS或类似方式提供数据.值得思考您拥有的数据类型,因为这些IPC机制具有不同的性能特征.有一份USENIX论文草案,对各种值得一读的权衡进行了很好的分析.
既然你说(在这个答案的评论中)你更喜欢使用SHM,这里有一些代码示例来启动你.使用Python posix_ipc库:
import posix_ipc # POSIX-specific IPC
import mmap # From Python stdlib
class SharedMemory(object):
"""Python interface to shared memory.
The create argument tells the object to create a new SHM object,
rather than attaching to an existing one.
"""
def __init__(self, name, size=posix_ipc.PAGE_SIZE, create=True):
self.name = name
self.size = size
if create:
memory = posix_ipc.SharedMemory(self.name, posix_ipc.O_CREX,
size=self.size)
else:
memory = posix_ipc.SharedMemory(self.name)
self.mapfile = mmap.mmap(memory.fd, memory.size)
os.close(memory.fd)
return
def put(self, item):
"""Put item in shared memory.
"""
# TODO: Deal with the case where len(item) > size(self.mapfile)
# TODO: Guard this method with a named semaphore
self.mapfile.seek(0)
pickle.dump(item, self.mapfile, protocol=2)
return
def get(self):
"""Get a Python object from shared memory.
"""
# TODO: Deal with the case where len(item) > size(self.mapfile)
# TODO: Guard this method with a named semaphore
self.mapfile.seek(0)
return pickle.load(self.mapfile)
def __del__(self):
try:
self.mapfile.close()
memory = posix_ipc.SharedMemory(self.name)
memory.unlink()
except:
pass
return
Run Code Online (Sandbox Code Playgroud)
对于Java端,你想创建相同的类,尽管我在评论中说过,JTux似乎提供了相同的功能,你需要的API是UPosixIPC类.
下面的代码概述了您需要实现的类型.但是,有几个缺失 - 异常处理是显而易见的,也有一些标志(在UConstant中找到它们),你需要添加一个信号量来保护put/ get方法.但是,这应该让你走上正轨.请记住,mmap内存映射文件是一段RAM的文件类接口.因此,您可以使用其文件描述符,就好像它是fd普通文件一样.
import jtux.*;
class SHM {
private String name;
private int size;
private long semaphore;
private long mapfile; // File descriptor for mmap file
/* Lookup flags and perms in your system docs */
public SHM(String name, int size, boolean create, int flags, int perms) {
this.name = name;
this.size = size;
int shm;
if (create) {
flags = flags | UConstant.O_CREAT;
shm = UPosixIPC.shm_open(name, flags, UConstant.O_RDWR);
} else {
shm = UPosixIPC.shm_open(name, flags, UConstant.O_RDWR);
}
this.mapfile = UPosixIPC.mmap(..., this.size, ..., flags, shm, 0);
return;
}
public void put(String item) {
UFile.lseek(this.mapfile(this.mapfile, 0, 0));
UFile.write(item.getBytes(), this.mapfile);
return;
}
public String get() {
UFile.lseek(this.mapfile(this.mapfile, 0, 0));
byte[] buffer = new byte[this.size];
UFile.read(this.mapfile, buffer, buffer.length);
return new String(buffer);
}
public void finalize() {
UPosix.shm_unlink(this.name);
UPosix.munmap(this.mapfile, this.size);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13213 次 |
| 最近记录: |