200行自定義python異步非阻塞Web框架
Python的Web框架中Tornado以異步非阻塞而聞名。本篇將使用200行代碼完成一個(gè)微型異步非阻塞Web框架:Snow。
一、源碼
本文基于非阻塞的Socket以及IO多路復(fù)用從而實(shí)現(xiàn)異步非阻塞的Web框架,其中便是眾多異步非阻塞Web框架內(nèi)部原理。
#!/usr/bin/env python # -*- coding:utf-8 -*- import re import socket import select import time class HttpResponse(object): """ 封裝響應(yīng)信息 """ def __init__(self, content=''): self.content = content self.headers = {} self.cookies = {} def response(self): return bytes(self.content, encoding='utf-8') class HttpNotFound(HttpResponse): """ 404時(shí)的錯(cuò)誤提示 """ def __init__(self): super(HttpNotFound, self).__init__('404 Not Found') class HttpRequest(object): """ 用戶封裝用戶請(qǐng)求信息 """ def __init__(self, conn): self.conn = conn self.header_bytes = bytes() self.header_dict = {} self.body_bytes = bytes() self.method = "" self.url = "" self.protocol = "" self.initialize() self.initialize_headers() def initialize(self): header_flag = False while True: try: received = self.conn.recv(8096) except Exception as e: received = None if not received: break if header_flag: self.body_bytes += received continue temp = received.split(b'\r\n\r\n', 1) if len(temp) == 1: self.header_bytes += temp else: h, b = temp self.header_bytes += h self.body_bytes += b header_flag = True @property def header_str(self): return str(self.header_bytes, encoding='utf-8') def initialize_headers(self): headers = self.header_str.split('\r\n') first_line = headers[0].split(' ') if len(first_line) == 3: self.method, self.url, self.protocol = headers[0].split(' ') for line in headers: kv = line.split(':') if len(kv) == 2: k, v = kv self.header_dict[k] = v class Future(object): """ 異步非阻塞模式時(shí)封裝回調(diào)函數(shù)以及是否準(zhǔn)備就緒 """ def __init__(self, callback): self.callback = callback self._ready = False self.value = None def set_result(self, value=None): self.value = value self._ready = True @property def ready(self): return self._ready class TimeoutFuture(Future): """ 異步非阻塞超時(shí) """ def __init__(self, timeout): super(TimeoutFuture, self).__init__(callback=None) self.timeout = timeout self.start_time = time.time() @property def ready(self): current_time = time.time() if current_time > self.start_time + self.timeout: self._ready = True return self._ready class Snow(object): """ 微型Web框架類 """ def __init__(self, routes): self.routes = routes self.inputs = set() self.request = None self.async_request_handler = {} def run(self, host='localhost', port=9999): """ 事件循環(huán) :param host: :param port: :return: """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind((host, port,)) sock.setblocking(False) sock.listen(128) sock.setblocking(0) self.inputs.add(sock) try: while True: readable_list, writeable_list, error_list = select.select(self.inputs, [], self.inputs,0.005) for conn in readable_list: if sock == conn: client, address = conn.accept() client.setblocking(False) self.inputs.add(client) else: gen = self.process(conn) if isinstance(gen, HttpResponse): conn.sendall(gen.response()) self.inputs.remove(conn) conn.close() else: yielded = next(gen) self.async_request_handler[conn] = yielded self.polling_callback() except Exception as e: pass finally: sock.close() def polling_callback(self): """ 遍歷觸發(fā)異步非阻塞的回調(diào)函數(shù) :return: """ for conn in list(self.async_request_handler.keys()): yielded = self.async_request_handler[conn] if not yielded.ready: continue if yielded.callback: ret = yielded.callback(self.request, yielded) conn.sendall(ret.response()) self.inputs.remove(conn) del self.async_request_handler[conn] conn.close() def process(self, conn): """ 處理路由系統(tǒng)以及執(zhí)行函數(shù) :param conn: :return: """ self.request = HttpRequest(conn) func = None for route in self.routes: if re.match(route[0], self.request.url): func = route[1] break if not func: return HttpNotFound() else: return func(self.request) snow.py
二、使用
1. 基本使用
from snow import Snow from snow import HttpResponse def index(request): return HttpResponse('OK') routes = [ (r'/index/', index), ] app = Snow(routes) app.run(port=8012)
2.異步非阻塞:超時(shí)
from snow import Snow from snow import HttpResponse from snow import TimeoutFuture request_list = [] def async(request): obj = TimeoutFuture(5) yield obj def home(request): return HttpResponse('home') routes = [ (r'/home/', home), (r'/async/', async), ] app = Snow(routes) app.run(port=8012)
3.異步非阻塞:等待
基于等待模式可以完成自定制操作
from snow import Snow from snow import HttpResponse from snow import Future request_list = [] def callback(request, future): return HttpResponse(future.value) def req(request): obj = Future(callback=callback) request_list.append(obj) yield obj def stop(request): obj = request_list[0] del request_list[0] obj.set_result('done') return HttpResponse('stop') routes = [ (r'/req/', req), (r'/stop/', stop), ] app = Snow(routes) app.run(port=8012)
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- python3中celery異步框架簡(jiǎn)單使用+守護(hù)進(jìn)程方式啟動(dòng)
- python 5個(gè)頂級(jí)異步框架推薦
- python異步Web框架sanic的實(shí)現(xiàn)
- 關(guān)于Python核心框架tornado的異步協(xié)程的2種方法詳解
- Python的Tornado框架實(shí)現(xiàn)異步非阻塞訪問數(shù)據(jù)庫(kù)的示例
- Python的Tornado框架的異步任務(wù)與AsyncHTTPClient
- Python的Twisted框架上手前所必須了解的異步編程思想
- Python的Tornado框架異步編程入門實(shí)例
- 簡(jiǎn)單介紹Python的Tornado框架中的協(xié)程異步實(shí)現(xiàn)原理
- 在Python的gevent框架下執(zhí)行異步的Solr查詢的教程
- python 常用的異步框架匯總整理
相關(guān)文章
Python 給定的經(jīng)緯度標(biāo)注在地圖上的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python 給定的經(jīng)緯度標(biāo)注在地圖上的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07理解python多線程(python多線程簡(jiǎn)明教程)
這篇文章主要介紹了理解python多線程,一個(gè)快速理解python多線程的簡(jiǎn)明教程,需要的朋友可以參考下2014-06-06python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家徹底理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09舉例講解Python設(shè)計(jì)模式編程中的訪問者與觀察者模式
這篇文章主要介紹了Python設(shè)計(jì)模式編程中的訪問者與觀察者模式,設(shè)計(jì)模式的制定有利于團(tuán)隊(duì)協(xié)作編程代碼的協(xié)調(diào),需要的朋友可以參考下2016-01-01使用python實(shí)現(xiàn)簡(jiǎn)單去水印功能
這篇文章主要為大家詳細(xì)介紹了使用python實(shí)現(xiàn)簡(jiǎn)單去水印功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05