這篇文章主要講解了flask怎么開啟多線程,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
在我之前解釋了flask如何支持多線程主要通過兩個(gè)類來實(shí)現(xiàn),LocalStack和Local,在Local中有兩個(gè)屬性,__storage__和__ident_func__,后者用來獲取線程id,從而區(qū)分不同線程發(fā)來的請求
這次要說的是flask如何開啟多線程
先從app.run()這個(gè)方法看起
def run(self, host=None, port=None, debug=None, **options): from werkzeug.serving import run_simple if host is None: host = '127.0.0.1' if port is None: server_name = self.config['SERVER_NAME'] if server_name and ':' in server_name: port = int(server_name.rsplit(':', 1)[1]) else: port = 5000 if debug is not None: self.debug = bool(debug) options.setdefault('use_reloader', self.debug) options.setdefault('use_debugger', self.debug) try: run_simple(host, port, self, **options) #會(huì)進(jìn)入這個(gè)函數(shù) finally: # reset the first request information if the development server # reset normally. This makes it possible to restart the server # without reloader and that stuff from an interactive shell. self._got_first_request = False