python-zstandard icon indicating copy to clipboard operation
python-zstandard copied to clipboard

Streaming sequentially through open socket not working

Open yutingy opened this issue 8 years ago • 1 comments

I am trying to send numpy arrays through an open socket:

server:

import zstandard as zstd
from numpy import *
import json_tricks as json
import socketserver as SocketServer
import sys

x = array([1,2,3,4,5,6,7,8,9])
y = array(['a','b','c','d','e','f','g','h','i','j'])
li = [x,y]

HOST, PORT = "localhost", 9999

class MyTCPSocketHandler(SocketServer.StreamRequestHandler):

    def handle(self):

        sock = self.wfile
        for i in range(2):
            val = li[i]
            enc = json.dumps(val).encode('utf-8')
            cctx = zstd.ZstdCompressor()
            with cctx.stream_writer(sock) as compressor:
                compressor.write(enc)
   
HOST, PORT = "localhost", 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPSocketHandler)
print('ok')
server.serve_forever()

client:

import socket, sys
import numpy
import zstandard as zstd

HOST, PORT = "localhost", 9999

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:

        sock.connect((HOST,PORT))
        sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
    
        mf = sock.makefile()

        for i in range(2):
                data = ''
                dctx = zstd.ZstdDecompressor()
                for chunk in dctx.read_to_iter(mf.buffer, read_size=2):
                    data += chunk.decode('utf-8')

                print(data)


finally:
        pass

Any read_size specified on the client end that isn't 1 will cause an error:

zstd.ZstdError: zstd decompress error: Unknown frame descriptor. 

In this case, the first array is sent and received without problem, but the second (and any more, if there are more) will fail to transmit. In fact, when read_size=1 is specified, the size of the chunk is usually closer to 10kb. I want a chunk size of 1mb but it does not seem to work.

When it comes to sending a larger amount of data, a different error occurs. Instead of receiving this error message the client will hang while it receives. After closing the connection on the server side (by terminating the program), then sometimes the client will suddenly receive all the data. Is there a flush() call being missed in the zstandard code?

If it helps, everything here works perfectly fine if I replace the zstandard library with lz4framed.

yutingy avatar Aug 14 '18 15:08 yutingy

I think the problem here may be the use of mf.buffer in the read_to_iter() method. mf.buffer is presumably a buffer on the underlying file object. When we attempt to read(2) from that, I'm guessing it will read only the data that has been buffered so far.

Can you try passing mf into read_to_buffer() instead?

Also, since sock.makefile() is introducing its own I/O buffering layer, that is the thing actually reading from the socket: not zstandard. zstandard will read from that socket file (in chunks of 2 bytes in your example since read_size=2) and that socket file will recv() from the socket. If you want to limit the socket read size, you need to pass an argument to sock.makefile() to control the buffer size. But I'm not sure if buffer size is equivalent to read size: they may be different.

If you insist on control over the socket read behavior, you should do that I/O directly in Python and feed the raw data to a ZstdDecompressor using a different decompression API.

indygreg avatar Aug 14 '18 20:08 indygreg