Open pythonzm opened 5 years ago
Thanks for the report.
I kind of wanted to keep the interface streamlined for send()
and receive()
to be handling strings. The conversion can be done from the caller (i.e. convert the data to str before calling client.send()
) which adds a little bit of overhead of course.
The other solution would be for send
to accept bytes and str. I have done a quick benchmarking on the overhead incurred on each instruction (assuming the baseline is passing data: str
):
def send(data):
return data.encode()
def send_cond(data):
if isinstance(data, str):
return data.encode()
return data
assuming we are passing data: str
... timeit setup removed for brevity ...
>>> send.timeit()
>>> 0.2808417819906026
>>> send_cond.timeit()
>>> 0.540613093005959
thanks for your project!
i'm using it with django, and an error occurred while I was running the program:bytes has no attribute encode 。so i changed the code of client.py at line 122. before:
self.client.sendall(data.encode())
after:
if isinstance(data, bytes): self.client.sendall(data) elif isinstance(data, str): self.client.sendall(data.encode())
and then the program runs well.
i hope this issue will help