S
Skink
Hi,
I'm preparing a python server that sends java classes and resources to
custom java class loader. In order to make it faster I don't want to use
URLClassLoader that uses HTTP protocol 1.0 and for each class/resource
creates own connection.
Instead I'd like to use raw sockets with simple protocol:
- class loader sends a line terminated with \n with resource to get
- python server reads that line, gets the file and sends back an
integer with file length and then the file itself
- class loader reads a lenght integer and then reads the remainig data
The problem is when I try to read several files the first one is read
quite fast, but the rest is read 40 x slower. For example (time is in
seconds):
% python client.py client.py client.py client.py server.py server.py
init 0.00066089630127
client.py 0.000954866409302
client.py 0.0408389568329
client.py 0.0409188270569
server.py 0.0409059524536
server.py 0.0409259796143
what's wrong here?
thanks,
skink
client.py
------------------------------------------------------------------------------------
import socket, sys, struct, time
HOST = 'localhost'
PORT = 8080
t1 = time.time()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
t2 = time.time()
print "init", t2-t1
for arg in sys.argv[1:]:
t1 = time.time()
s.send(arg + "\n")
len, = struct.unpack("!i", s.recv(4))
data = s.recv(len)
t2 = time.time()
print arg, t2-t1
s.close()
------------------------------------------------------------------------------------
server.py
------------------------------------------------------------------------------------
import socket, struct, binascii
HOST = ''
PORT = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
while 1:
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
f = conn.makefile()
while 1:
resource = f.readline().rstrip()
print "[%s]" % resource
if not resource:
break
data = open(resource, "rb").read()
conn.sendall(struct.pack("!i", len(data)))
conn.sendall(data)
conn.close()
------------------------------------------------------------------------------------
I'm preparing a python server that sends java classes and resources to
custom java class loader. In order to make it faster I don't want to use
URLClassLoader that uses HTTP protocol 1.0 and for each class/resource
creates own connection.
Instead I'd like to use raw sockets with simple protocol:
- class loader sends a line terminated with \n with resource to get
- python server reads that line, gets the file and sends back an
integer with file length and then the file itself
- class loader reads a lenght integer and then reads the remainig data
The problem is when I try to read several files the first one is read
quite fast, but the rest is read 40 x slower. For example (time is in
seconds):
% python client.py client.py client.py client.py server.py server.py
init 0.00066089630127
client.py 0.000954866409302
client.py 0.0408389568329
client.py 0.0409188270569
server.py 0.0409059524536
server.py 0.0409259796143
what's wrong here?
thanks,
skink
client.py
------------------------------------------------------------------------------------
import socket, sys, struct, time
HOST = 'localhost'
PORT = 8080
t1 = time.time()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
t2 = time.time()
print "init", t2-t1
for arg in sys.argv[1:]:
t1 = time.time()
s.send(arg + "\n")
len, = struct.unpack("!i", s.recv(4))
data = s.recv(len)
t2 = time.time()
print arg, t2-t1
s.close()
------------------------------------------------------------------------------------
server.py
------------------------------------------------------------------------------------
import socket, struct, binascii
HOST = ''
PORT = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
while 1:
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
f = conn.makefile()
while 1:
resource = f.readline().rstrip()
print "[%s]" % resource
if not resource:
break
data = open(resource, "rb").read()
conn.sendall(struct.pack("!i", len(data)))
conn.sendall(data)
conn.close()
------------------------------------------------------------------------------------