Skip to content

Commit

Permalink
Speed up receiving data
Browse files Browse the repository at this point in the history
Avoid doing bytes concatenation if we can.
  • Loading branch information
iamsrp-deshaw committed Oct 28, 2024
1 parent 2b8781a commit 122cc46
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions examples/array_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,22 @@ def write():
count = sz
flat = dst.reshape((src.size,))
end = len(flat)
data = list()
for idx in range(0, end, sz):
e = idx + sz
if e > end:
e = end
count = e - idx
data = b''
while len(data) < count * 8:
data += sock.recv(count * 8 - len(data))
flat[idx:e] = numpy.frombuffer(data, dtype='float64', count=count)
want = count * 8
data.clear()
while want > 0:
d = sock.recv(want)
want -= len(d)
data.append(d)
if len(data) == 1:
flat[idx:e] = numpy.frombuffer(data[0], dtype='float64', count=count)
else:
flat[idx:e] = numpy.frombuffer(b''.join(data), dtype='float64', count=count)

try:
ss.close()
Expand Down

0 comments on commit 122cc46

Please sign in to comment.