Decoding a json response takes a lot of time on my system - Precision 5520 (11th gen I7, 64 GB) with Linux 20.04 LTS under WSL2 under Windows 10 Pro. Here is the relevant part of the code:
def ms_now():
return int(time.time_ns() / 1000000)
class Timer():
def __init__(self):
self.start = ms_now()
def stop(self):
return ms_now() - self.start
timer = Timer()
response = client.invoke(
FunctionName='my-aws-lambda-function',
Payload=json.dumps({'body': texts}),
)
print("response", timer.stop(), flush=True)
payload_json = response['Payload']
print("payload_json", timer.stop(), flush=True)
result = json.load(payload_json)
print("result", timer.stop(), flush=True)
This code invokes an AWS Lambda function. The response consists of 38,400 floats. Here is an output of one run (the times are in milliseconds):
response 5619
payload_json 5620
result 76314
It takes more than a minute to deserialize that response which is not that large by any means. I ran this same code in the terminal of PythonAnywhere and the deserialization took 100 milliseconds.
I suspect that WSL is the culprit. How do I diagnose and fix this problem?
EDIT
- The same script works fine in the Windows host:
response 1934
payload_json 1934
result 3239
- Since people seem to assume that it's my connection that's slow in WSL, note that when I install packages with large dependencies, such as HuggingFace's
sentence-transformers
, usingpip
, they are downloaded quite fast.
payload_json
? Can you separately measuredata = payload_json.read()
andjson.loads(data)
? I'm betting it's the former that will be slow, not the latter.json.loads
and it was fast. So I guess it's about reading the i/o buffer through WSL2. How do I fix it?repr(data)
say about it?boto3
for making request to AWS and receives the response, which was obviously transferred over the network. What isdata
in my code?