本文介绍了使用Python BaseHTTPServer处理同时/异步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过创建一个继承自HTTPServer和ThreadingMixIn的类来设置线程化(带有Python线程)的HTTP服务器:

I've set up a threaded (with Python threads) HTTP server by creating a class that inherits from HTTPServer and ThreadingMixIn:

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    pass

我有一个继承自BaseHTTPRequestHandler的处理程序类,我用类似这样的东西启动服务器:

I have a handler class which inherits from BaseHTTPRequestHandler, and I start the server with something like this:

class MyHandler(BaseHTTPRequestHandler):
    ...

server = ThreadedHTTPServer(('localhost', 8080), MyHandler)
# Prevent issues with socket reuse
server.allow_reuse_address = True
# Start the server
server.serve_forever()

这一切都非常简单.我遇到的问题是ThreadingMixIn,ForkingMixIn或其他情况,请求结束了对请求处理程序的阻塞以返回.通过实现以下示例代码,可以很容易地看出这一点:

This is all pretty straightforward. The problem that I'm encountering is that, ThreadingMixIn, ForkingMixIn, or otherwise, the request winds up blocking on the request handler to return. This can easily be seen by implementing this example code:

class MyHandler(BaseHTTPRequestHandler):
    def respond(self, status_code):
        self.send_response(status_code)
        self.end_headers()

    def do_GET(self):
         print "Entered GET request handler"
         time.sleep(10)
         print "Sending response!"
         respond(200)

如果服务器正在同时处理这些请求,那么我们将能够发送两个请求,并看到服务器在发送任何一个响应之前都输入了两个GET请求处理程序.相反,服务器将为第一个请求输入GET请求处理程序,等待它返回,然后为第二个请求输入(因此第二个请求大约需要20秒才能返回,而不是10秒).

If the server were processing these simultaneously, then we would be able to send two requests and see the server enter both GET request handlers before sending either response. Instead, the server will enter the GET request handler for the first request, wait for it to return, then enter it for the second (so the second request takes ~20 seconds to return instead of 10).

我是否可以通过一种简单的方法来实现一个系统,使服务器不等待处理程序返回?具体来说,我正在尝试编写一个系统,该系统等待接收到多个请求,然后再返回其中的任何一个(长时间轮询的一种形式),并且遇到了第一个请求等待阻止任何将来的请求连接到服务器的问题.

Is there a straightforward way for me to implement a system where the server doesn't wait on the handler to return? Specifically, I'm trying to write a system which waits to receive several requests before returning any of them (a form of long polling) and running into issues where the first request waiting blocks any future requests from connecting to the server.

推荐答案

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    pass

就足够了.您的客户可能不会发出并发请求.如果并行发出请求,则线程服务器将按预期工作.这是客户:

is enough. Your client probably don't make concurrent requests. If you make the requests in parallel the threaded server works as expected. Here's the client:

#!/usr/bin/env python
import sys
import urllib2

from threading import Thread

def make_request(url):
    print urllib2.urlopen(url).read()

def main():
    port = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
    for _ in range(10):
        Thread(target=make_request, args=("http://localhost:%d" % port,)).start()

main()

以及相应的服务器:

import time
from BaseHTTPServer   import BaseHTTPRequestHandler, HTTPServer, test as _test
from SocketServer     import ThreadingMixIn


class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    pass

class SlowHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()

        self.wfile.write("Entered GET request handler")
        time.sleep(1)
        self.wfile.write("Sending response!")

def test(HandlerClass = SlowHandler,
         ServerClass = ThreadedHTTPServer):
    _test(HandlerClass, ServerClass)


if __name__ == '__main__':
    test()

所有10个请求均在1秒内完成.如果从服务器定义中删除ThreadingMixIn,则所有10个请求都将花费10秒来完成.

All 10 requests finish in 1 second. If you remove ThreadingMixIn from the server definition then all 10 requests take 10 seconds to complete.

这篇关于使用Python BaseHTTPServer处理同时/异步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:31