是的,serve_forever()
阻止了这一切。您可以使用handle_request
一次服务一个请求。为确保它不会阻塞,您必须设置超时。要定期运行它,您可以使用tornado.ioloop.PeriodicCallback
. 例子:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
message = threading.currentThread().getName()
self.wfile.write(message)
self.wfile.write('\n')
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
class IndexHandler(tornado.web.RequestHandler):
def get(self):
greeting = self.get_argument('greeting', 'Hello')
self.write(greeting + ', friendly user!\n')
if __name__ == '__main__':
# create Tornado Server
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
# create BaseHTTPServer
server = ThreadedHTTPServer(('localhost', 8080), Handler)
server.timeout = 0.01
tornado.ioloop.PeriodicCallback(server.handle_request, 100).start() # every 100 milliseconds
tornado.ioloop.IOLoop.instance().start()
跑步:
$ curl http://localhost:8080/
Thread-1
$ curl http://localhost:8080/
Thread-2
$ curl http://localhost:8000/
Hello, friendly user!
$ curl http://localhost:8080/
Thread-3
$ curl http://localhost:8000/
Hello, friendly user!
$ curl http://localhost:8080/
Thread-4
$ curl http://localhost:8000/
Hello, friendly user!
$ curl http://localhost:8000/
Hello, friendly user!
我在这里使用timeout
属性来设置超时。我不确定这是否是正确的方法。其他方法:http ://code.activestate.com/recipes/499376/
另一种解决方案:在自己的线程中运行每个服务器:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
message = threading.currentThread().getName()
self.wfile.write(message)
self.wfile.write('\n')
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
class IndexHandler(tornado.web.RequestHandler):
def get(self):
greeting = self.get_argument('greeting', 'Hello')
self.write(greeting + ', friendly user!\n')
def run_tornado():
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
def run_base_http_server():
server = ThreadedHTTPServer(('localhost', 8080), Handler)
server.serve_forever()
if __name__ == '__main__':
threading.Thread(target=run_tornado).start()
threading.Thread(target=run_base_http_server).start()