不容易得到一个Hello World网页。你可以有两个ThreadingHTTPServer实例,编写你自己的serve_forever()函数(不要担心它不是一个复杂的函数)。
现有的功能:
def serve_forever(self, poll_interval=0.5):
"""Handle one request at a time until shutdown.
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.
"""
self.__serving = True
self.__is_shut_down.clear()
while self.__serving:
# XXX: Consider using another file descriptor or
# connecting to the socket to wake this up instead of
# polling. Polling reduces our responsiveness to a
# shutdown request and wastes cpu at all other times.
r, w, e = select.select([self], [], [], poll_interval)
if r:
self._handle_request_noblock()
self.__is_shut_down.set()
所以我们更换会是这样的:
def serve_forever(server1,server2):
while True:
r,w,e = select.select([server1,server2],[],[],0)
if server1 in r:
server1.handle_request()
if server2 in r:
server2.handle_request()
GIL可以吗? – sashab 2012-04-03 14:40:47
@scrat:GIL对于这段代码无关紧要,因为这段代码大部分都是I/O绑定的,而且Python中的大部分I/O都是使用释放GIL的低级C库编写的。与大多数性能问题一样,我的建议是不要担心它,除非您已经对代码进行了基准测试并确定它确实是一个问题。 – 2012-04-03 15:24:42