Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does bottle handle requests with no concurrency?

Tags:

python

bottle

At first, I think Bottle will handle requests concurrently, so I wrote test code bellow:

import json
from bottle import Bottle, run, request, response, get, post
import time

app = Bottle()
NUMBERS = 0


@app.get("/test")
def test():
    id = request.query.get('id', 0)
    global NUMBERS
    n = NUMBERS
    time.sleep(0.2)
    n += 1
    NUMBERS = n
    return id


@app.get("/status")
def status():
    return json.dumps({"numbers": NUMBERS})


run(app, host='0.0.0.0', port=8000)

Then I use jmeter to request /test url with 10 threads loops 20 times.

After that, /status gives me {"numbers": 200}, which seems like that bottle does not handle requests concurrently.

Did I misunderstand anything?

UPDATE

I did another test, I think it can prove that bottle deal with requests one by one(with no concurrency). I did a little change to the test function:

@app.get("/test")
def test():
    t1 = time.time()
    time.sleep(5)
    t2 = time.time()
    return {"t1": t1, "t2": t2}

And when I access /test twice in a browser I get:

{
    "t2": 1415941221.631711,
    "t1": 1415941216.631761
}
{
    "t2": 1415941226.643427,
    "t1": 1415941221.643508
}
like image 381
WKPlus Avatar asked Nov 14 '14 04:11

WKPlus


People also ask

Does Flask support concurrency?

Quart is a reimplementation of Flask based on the ASGI standard instead of WSGI. This allows it to handle many concurrent requests, long running requests, and websockets without requiring multiple worker processes or threads.

Can Flask only handle one request at a time?

After seeing what just happened, next time people say flask is synchronous and it can handle only one request at a time. Knowing that by configuring thread, process at app. run, you know for sure we will be able to handle more than a request at a time.

Can Python handle concurrent requests?

Both multithreading and multiprocessing allow Python code to run concurrently. Only multiprocessing will allow your code to be truly parallel. However, if your code is IO-heavy (like HTTP requests), then multithreading will still probably speed up your code.

How many concurrent users can Flask handle?

Flask will process one request per thread at the same time. If you have 2 processes with 4 threads each, that's 8 concurrent requests. Flask doesn't spawn or manage threads or processes.


1 Answers

Concurrency isn't a function of your web framework -- it's a function of the web server you use to serve it. Since Bottle is WSGI-compliant, it means you can serve Bottle apps through any WSGI server:

  • wsgiref (reference server in the Python stdlib) will give you no concurrency.
  • CherryPy dispatches through a thread pool (number of simultaneous requests = number of threads it's using).
  • nginx + uwsgi gives you multiprocess dispatch and multiple threads per process.
  • Gevent gives you lightweight coroutines that, in your use case, can easily achieve C10K+ with very little CPU load (on Linux -- on Windows it can only handle 1024 simultaneous open sockets) if your app is mostly IO- or database-bound.

The latter two can serve massive numbers of simultaneous connections.

According to http://bottlepy.org/docs/dev/api.html , when given no specific instructions, bottle.run uses wsgiref to serve your application, which explains why it's only handling one request at once.

like image 60
Max Noel Avatar answered Sep 30 '22 19:09

Max Noel