不容易得到一個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