简体   繁体   中英

Python HTTP Request/ Response very slow speed

I want to test how much http request an easy ping-pong service can handle in a certain amount of time. I have already implemented it in Java and Go and it works fine. But when testing it with python one single ping-pong cycle needs a bit more than 2s on my machine and that's enormously long. In the other languages I have nearly 1 per each milliseconds. More than 2000 times what I get with python... how can that be? I think the http server needs to be build up each time again with python, but is it really that and how could I fix it?

And of course the code to my little python scripts is down below:

import requests

add = 'aaaaaaaaaa'
sum = ''

def getTime(i):
    site_request = requests.post("http://localhost:8082/ping", data=i)
    site_response = str(site_request.content)
    return  site_response

def printToFile():
    return

for x in range(5):
    print(getTime(sum))
    sum += add
from flask import Flask
from flask import request
import requests
import time

app = Flask(__name__)

@app.route("/ping", methods=['POST'])
def region():
   data = request.get_data()
   start = time.time()
   site_request = requests.post("http://localhost:8080/pong", data=data)
   site_response = str(site_request.content)
   end = time.time()
   cl = int(site_request.headers['content-length'])
   if cl != len(data):
      return end - start + ", " + cl + "," + 500

   return str(end - start) + "," + str(cl) + "," + str(200)

app.run(host='127.0.0.1', port= 8082)
from flask import Flask
from flask import request

app = Flask(__name__)

@app.route("/pong", methods=['POST'])
def region():
   data = request.get_data()
   return data

app.run(host='127.0.0.1', port= 8080)

The problem you are facing here is that the Python requests library is a synchronous library.

While your Flask app (if set up correctly) will be capable of handling many requests at once, your code used to send the requests will only send one at a time and will block until each one is finished in sequence.

A better test of the speed of your server will be to use Python's somewhat newer async features utilizing a library like asyncio

Give that a try as the method for firing off your test requests and see if it is still slow. If so, at least you will have ruled out one potential issue!

As an example of an excellent library you could try that utilizes asyncio, look at AIOHTTP .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM