首先,我根本不是异步专家,我只是研究过这个话题一次。
IMHO如果您使用XAMPP,那么您将失去执行长轮询的可能性,因为Apache对每个请求使用线程/进程(取决于配置)。在
您需要的是非阻塞的web服务器,比如Tornado,它允许将请求分成两部分,其中第二部分在某个事件中被触发,但同时服务器可以接受后续的入站请求。在class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
http = tornado.httpclient.AsyncHTTPClient()
http.fetch("http://friendfeed-api.com/v2/feed/bret",
callback=self.async_callback(self.on_response))
def on_response(self, response):
if response.error: raise tornado.web.HTTPError(500)
json = tornado.escape.json_decode(response.body)
self.write("Fetched " + str(len(json["entries"])) + " entries "
"from the FriendFeed API")
self.finish()
据我所知,在Apache下这是不可能的-在Apache中,fetch是请求处理程序的常规部分,当然,在它完成之前,它会被阻塞-所以您最终得到的是冻结的线程或进程。在
另一个在Python中执行非阻塞服务的著名库是Twisted,但我对它了解不多,只知道它还能够帮助您处理许多只使用一个线程/进程的连接。在