首先,我根本不是异步专家,我只研究了一次该主题。恕我直言,如果您使用的是XAMPP,那么您就失去了进行长时间轮询的可能性,因为Apache为每个请求使用线程/进程(取决于配置)。
您需要的是非阻塞式Web服务器,例如Tornado,它可以将请求分为两部分,其中第二部分在某个事件时触发,但同时服务器可以接受后续的入站请求。
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_depre(response.body) self.write("Fetched " + str(len(json["entries"])) + " entries " "from the FriendFeed API") self.finish()
-据我所知,在Apache下这是不可能的-在fetch中,获取是请求处理程序的常规部分,当然会阻塞直到完成为止-因此,最终的结果是冻结线程或进程。
另一个著名的在Python中执行非阻塞服务的库是Twisted,但是我对其了解不多,只是它还能够帮助您仅使用一个线程/进程来处理许多连接。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)