Does bottle handle requests with no concurrency?

Deadly 提交于 2019-12-03 12:53:12

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/26923101/does-bottle-handle-requests-with-no-concurrency

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!