Web.py:无法通过多个浏览器选项卡获得多线程行为

发布于 2024-10-26 15:34:24 字数 653 浏览 2 评论 0原文

我听说 Web.py 默认是一个多线程网络服务器。因此创建了一个简单的应用程序,在返回“Hello World”之前休眠一分钟。

现在,如果我从两个不同的浏览器<调用此应用程序(即http://localhost:8080/) /strong> 几乎同时,60 秒后我几乎同时在两个浏览器中收到“Hello World” - 表明 Web.py 端的多线程工作正常。

但是,如果我在同一个浏览器中打开两个选项卡并几乎同时调用相同的网址(相隔几秒钟),则按预期在 60 秒后我会在第一个选项卡中收到“Hello world”,然后收到“Hello world”第一次响应后 60 秒,第二个选项卡中显示“世界”。总共 120 秒。由此表明Web.py没有做多线程。

我想最终创建一个 python 客户端应用程序(使用 httplib2),它将从不同的线程发出 http 请求。来自不同线程的那些http请求可能完全相同。我假设这或多或少类似于从同一浏览器中的不同选项卡发出 http 请求。

关于如何在这种情况下获得多线程行为有什么想法吗?或者我做错了什么? web.py 需要任何特殊配置吗?或者任何其他(简单的)网络框架都可以满足我的期望。

I heard Web.py is a multithreaded webserver by default. So created a simple application that sleeps for a minute before returning "Hello World".

Now if I call this application (i.e., http://localhost:8080/) from two different browsers almost simultaneously, I get the 'Hello World' almost simultaneously in both browsers after 60 seconds - indicating that multithreading at the Web.py end is working fine.

But if I open two tabs in same browser and call the same url almost simultaneously (a few seconds apart), I get the "Hello world" in first tab after 60 seconds as expected and then the "Hello World" in 2nd tab 60 seconds after the first response. That is total of 120 seconds. Thus indicating that Web.py did not do multithreading.

I want to eventually create a python client application (using httplib2) that will issue http requests from different threads. Those http requests from the different threads may be exactly the same. I am assuming that is more or less similar to issuing http requests from different tabs in the same browser.

Any ideas on how to get multi-threading behavior in this case ? Or what I am doing wrong? Any special configuration of web.py needed ? Or any other (simple) web framework that will do what I expect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久夏青 2024-11-02 15:34:24

您描述的行为似乎特定于某些浏览器。在花时间重新创建您的情况后,即创建一个简单的 web.py 应用程序,在回答请求之前休眠一段时间,我能够在 Firefox 中重新创建该问题。在 IE8 中使用两个选项卡进行相同的尝试实际上产生了最初预期的结果:两个请求同时处理。

这让我相信问题与浏览器相关,而不是 web.py 的问题。最有可能的是,某些浏览器会将向同一 URL 或域发出的请求排队,而不是一次发送所有请求。

多线程或多进程 Python 应用程序不应遇到同样的问题。

出于参考目的,这是我使用基本教程制作的简单 web.py 应用程序:

#!/usr/bin/env python

import time, web

urls = (
  '/', 'index'
)

web.config.debug = False
app = web.application(urls, globals())

class index:
    def GET(self):
        time.sleep(10)
        return "Hello, world!"

if __name__ == "__main__":
    app.run()

The behaviour you describe seems to be specific to certain browsers. After taking the time to recreate your situation, i.e. creating a simple web.py application that sleeps for a while before answering a request, I was able to recreate the issue - in Firefox. Trying the same in IE8 using two tabs actually produced the originally expected result: both requests are processed simultaneously.

This leads me to believe that the problem is browser related, not an issue with web.py. Most likely, some browsers will queue requests made to the same URL or domain instead of sending them all at once.

A multithreaded or multiprocessed Python application should not suffer from the same problem.

For reference purposes, this is the simple web.py app I produced, using the basic tutorial:

#!/usr/bin/env python

import time, web

urls = (
  '/', 'index'
)

web.config.debug = False
app = web.application(urls, globals())

class index:
    def GET(self):
        time.sleep(10)
        return "Hello, world!"

if __name__ == "__main__":
    app.run()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文