当调用相同的端点时,带有CherryPy的瓶子不会表现出多线程的行为 [英] Bottle with CherryPy does not behave multi-threaded when same end-point is called

查看:118
本文介绍了当调用相同的端点时,带有CherryPy的瓶子不会表现出多线程的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Bottle与CherryPy服务器一起使用时应表现为多线程.我有一个简单的测试程序:

As far as I know Bottle when used with CherryPy server should behave multi-threaded. I have a simple test program:

from bottle import Bottle, run
import time

app = Bottle()

@app.route('/hello')
def hello():
    time.sleep(5)

@app.route('/hello2')
def hello2():
    time.sleep(5)

run(app, host='0.0.0.0', server="cherrypy", port=8080)

当我通过打开2个选项卡并同时刷新它们来调用localhost:8080/hello时,它们不会同时返回,但是其中一个在5秒钟后完成,另一个在5秒钟后完成

When I call localhost:8080/hello by opening 2 tabs and refreshing them at the same time, they don't return at the same time but one of them is completed after 5 seconds and the other is completed after 5 more seconds.

但是当我在一个选项卡中同时调用/hello并在另一个选项卡中同时调用/hello2时,它们同时完成.

But when I call /hello in one tab and /hello2 in another at the same time they finish at the same time.

为什么同一端点被两次调用时Bottle不表现多线程?有没有办法使其成为多线程的?

Why does Bottle not behave multi-threaded when the same end-point is called twice? Is there a way to make it multi-threaded?

Python版本:2.7.6

Python version: 2.7.6

瓶装版本:0.12.8

Bottle version: 0.12.8

CherryPy版本:3.7.0

CherryPy version: 3.7.0

OS:在Ubuntu 14.04 64位& Windows 10 64位

OS: Tried on both Ubuntu 14.04 64-Bit & Windows 10 64-Bit

推荐答案

我已经回答了一个问题这让我感到困惑.如果您会在附近搜索相关问题,则该列表会一直存在.

I already met this behaviour answering one question and it had gotten me confused. If you would have searched around for related questions the list would go on and on.

怀疑是服务器端对Keep-Alive,HTTP流水线,缓存策略等的处理不正确.但实际上它与服务器端完全无关.由于浏览器缓存的实现(Firefox,Chromium),对到达同一URL的并发请求进行了序列化. 最佳答案在直接搜索Bugtrackers之前找到的答案:

The suspect was some incorrect server-side handling of Keep-Alive, HTTP pipelining, cache policy or the like. But in fact it has nothing to do with server-side at all. The concurrent requests coming to the same URL are serialised because of a browser cache implementation (Firefox, Chromium). The best answer I've found before searching bugtrackers directly, says:

Necko 的缓存只能为每个缓存项处理一位作者.因此,如果您对同一URL发出多个请求,则第一个请求将打开用于写入的缓存条目,而后一个请求将在打开的缓存条目上受阻,直到第一个请求完成.

Necko's cache can only handle one writer per cache entry. So if you make multiple requests for the same URL, the first one will open the cache entry for writing and the later ones will block on the cache entry open until the first one finishes.

实际上,如果禁用Firebug或DevTools中的缓存,效果将不会持久.

Indeed, if you disable cache in Firebug or DevTools, the effect doesn't persist.

因此,如果您的客户端不是浏览器(例如API),请忽略该问题.否则,如果您确实确实需要从一个浏览器向同一URL发出并发请求(正常请求或XHR),请添加随机查询字符串参数以使请求URL唯一,例如http://example.com/concurrent/page?nocache=1433247395.

Thus, if your clients are not browsers, API for example, just ignore the issue. Otherwise, if you really need to do concurrent requests from one browser to the same URL (normal requests or XHRs) add random query string parameter to make request URLs unique, e.g. http://example.com/concurrent/page?nocache=1433247395.

这篇关于当调用相同的端点时,带有CherryPy的瓶子不会表现出多线程的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆