Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Tornado/Flask server choke and die on Windows when hammering it with requests?

I have a simple test application on Windows - Tornado running a Flask wsgi application. I can start the server just fine and connect through my web browser and that's just cool.

I can run my performance test, and on my machine I get ~900-1000 requests per second. However, after about 20,000 requests, my server stops responding and my test reports 0 per second. I can try to connect through the web browser, but nothing. Typically ctrl+c has some issues, too (having to hit it several times before refreshing the page in the browser will end the server like normally).

So why does my server choke and die when I hammer on it like this?

Okay, so in the process of trying to rule out different factors - I was able to hit the server from a different machine, even though my local machine was barfing so it looks like it's actually my local machine that's running out of ports or something?

Anyway, here's my code:

server.py

from flask import Flask

app = Flask(__name__)


@app.route("/")
def main():
    return "Hey, it's working"


if __name__ == "__main__":
    app.run("0.0.0.0", port=5000, debug=True)

tornado_server.py

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from server import app

http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)
IOLoop.instance().start()

perftest.py

from socket import *
import time

n = 0
stop = False

from threading import Thread
def monitor():
    global n, stop
    while not stop:
        time.sleep(1)
        print(n, 'reqs/sec')
        n = 0


if __name__ == "__main__":
    t = Thread(target=monitor).start()
    while True:
        try:
            sock = socket(AF_INET, SOCK_STREAM)
            sock.connect(('localhost', 5000))
            sock.send(b'''GET / HTTP/1.1\n\n''')
            resp = sock.recv(4096)
            sock.close()
            n += 1
        except KeyboardInterrupt:
            stop = True
        except:
            pass
like image 744
Wayne Werner Avatar asked Nov 09 '22 16:11

Wayne Werner


1 Answers

I guess you are running out of ports. Every connection you make blocks a port and it takes some time to close them, on Windows longer than on other operating systems. As a symptom is should work gain after some time, but only shortly.

You can check that with netstat.

like image 177
Klaus D. Avatar answered Nov 14 '22 21:11

Klaus D.