django多线程是什么[Django框架]
在一段完整的代码中,往往会有需要独立的代码模块,而这些独立运行的程序片段叫作“线程”(Thread),利用多个线程编程的概念就叫作多线程处理(多线程编程),多线程是为了同步完成多项任务,不是为了提高运行效率,而是为了提高资源使用效率来提高系统的效率。多线程是在程序在同一时间需要完成多项任务的时候实现的。多线程的目的仅仅是为了提高资源利用效率。各个线程执行自己的任务,这些线程可以”同时进行“。同时进行并非同一时刻进行,而是在某一时间段内,完成所有任务,任务的运行有先后顺序。
简单的说就是服务端监听 socket 每次 accept 一个新的请求后,开一个线程处理 这个 socket 客户连接。如果你对底层实现原理感兴趣,可以继续看下去,从 socket 编程的角度来解释多线程 wsgi server。最后附上一个异步框架工作过程的视频讲解。这里我们自己撸一个简单的多线程 wsgi server 来看下原理,还是需要深入源码和 socket 编程你才能真正理解。 我们从 python 自带的一个 wsgi server 看下如何实现多线程处理请求。首先你需要熟悉下 wsgi。 看一个最简单的 wsgi app:
def application(environ, start_response):status = '200 OK'
headers = [('Content-Type', 'text/html; charset=utf8')]
start_response(status, headers)
return [b"<h2>Hello</h2>"]if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('127.0.0.1', 8000, application)
httpd.serve_forever()
然后用你的开发工具跟进去 make_server 这个函数,看下它的定义:
def make_server(host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler):
"""Create a new WSGI server listening on `host` and `port` for `app`"""
server = server_class((host, port), handler_class)
server.set_app(app)
return serverclass WSGIServer(HTTPServer):
"""BaseHTTPServer that implements the Python WSGI protocol"""
application = None
def server_bind(self):
"""Override server_bind to store the server name."""
HTTPServer.server_bind(self)
self.setup_environ()
def setup_environ(self):
# Set up base environment
env = self.base_environ = {}
env['SERVER_NAME'] = self.server_name
env['GATEWAY_INTERFACE'] = 'CGI/1.1'
env['SERVER_PORT'] = str(self.server_port)
env['REMOTE_HOST']=''
env['CONTENT_LENGTH']=''
env['SCRIPT_NAME'] = ''
def get_app(self):
return self.application def set_app(self,application):
self.application = application
看到这个 WSGIServer 定义了吗,继承了一个 HttpServer。我们再继续追一下其定义:
class HTTPServer(SocketServer.TCPServer):allow_reuse_address = 1 # Seems to make sense in testing environment
def server_bind(self):
"""Override server_bind to store the server name."""
SocketServer.TCPServer.server_bind(self)
host, port = self.socket.getsockname()[:2]
self.server_name = socket.getfqdn(host)
self.server_port = port
到这里,我们继续追,看到 TcpServer 定义:
class TCPServer(BaseServer):"""这里我省略了定义"""
你还可以发现一个 ThreadingTCPServer 类:我们看下它的定义
class ThreadingTCPServer(ThreadingMixIn, TCPServer):pass
好了,怎么多线程处理请求呢?看下这个 ThreadingMixIn 类是如何实现的:
class ThreadingMixIn:"""Mix-in class to handle each request in a new thread."""
# Decides how threads will act upon termination of the
# main process
daemon_threads = False
def process_request_thread(self, request, client_address):
"""Same as in BaseServer but as a thread.
In addition, exception handling is done here.
"""
try:
self.finish_request(request, client_address)
self.shutdown_request(request)
except:
self.handle_error(request, client_address)
self.shutdown_request(request)
def process_request(self, request, client_address):
"""Start a new thread to process the request."""
t = threading.Thread(target = self.process_request_thread,
args = (request, client_address))
t.daemon = self.daemon_threads
t.start()
看到吗,其实就是对于每个新请求,会启动一个新的线程来处理 socket 请求。假如让我们自己实现一个多线程 socket server 应该怎么实现呢?先来写一个简单的单线程 socker echo server:
from socket import * # 偷懒这么写s = socket(AF_INET, SOCK_STREAM)s.bind(("", 9000))s.listen(5)while True:c, a = s.accept()
print "Received connection from", a
c.send("Hello %s
" % a[0])
c.close()
以上是 django多线程是什么[Django框架] 的全部内容, 来源链接: utcz.com/z/539746.html