From a07088cbe7c419e1ffb1ebf57e9ec7770d72f57c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AD=90=E6=98=82?= Date: Fri, 29 Dec 2023 15:16:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4closing=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 72 ++++++++++------------------------------------- pywss/__init__.py | 8 ++++-- pywss/closing.py | 20 ------------- 3 files changed, 20 insertions(+), 80 deletions(-) delete mode 100644 pywss/closing.py diff --git a/README.md b/README.md index 22b4479..bfa78cb 100644 --- a/README.md +++ b/README.md @@ -44,16 +44,27 @@ pip3 install pywss ### 2、搭建 web 应用 首先创建 `main.py` 文件,并写入以下代码: ```python +import time import pywss +def log_handler(ctx: pywss.Context): + start_time = time.time() + ctx.next() + print( + f"Route: {ctx.route}, " + f"Method: {ctx.method}, " + f"Status: {ctx.response_status_code}, " + f"Time: {time.time() - start_time:.3f}s" + ) + def handler(ctx: pywss.Context): ctx.write("hello~") -def main(port = 8080): +def main(): app = pywss.App() - app.get("/hi", lambda ctx: ctx.write("hi~")) # curl localhost:8080/hi - app.post("/hello", handler) # curl -X POST localhost:8080/hello - app.run(port=port) + app.get("/hello", handler) # curl localhost:8080/hello + app.any("*", log_handler, handler) # curl -X POST localhost:8080/hello + app.run() if __name__ == '__main__': main() @@ -66,56 +77,3 @@ python3 main.py 至此,一个简单的 web 应用服务就完成了。 更多功能见[在线文档](https://czasg.github.io/pywss/)。 - -
- -## 特性速览 - -### 轻巧的中间件机制 -```python -import time -import pywss - -# 请求日志中间件 -def logHandler(ctx: pywss.Context): - startTime = time.time() - ctx.next() - cost = time.time() - startTime - print(f"{ctx.method} - {ctx.route} - cost: {cost: .2f}") - -app = pywss.App() -app.use(logHandler) # 注册全局日志中间件 -app.run() -``` - -### 原生的依赖注入体验 -```python -import pywss - -class Repo: - def get(self): - return "repo" - -class Service: - - def __init__(self, repo: Repo): # Service 依赖 Repo - self.repo = repo - - def get(self): - return "power by " + self.repo.get() - -class UserView: - - def __init__(self, service: Service): # UserView 依赖 Service - self.srv = service - - def http_get(self, ctx): - ctx.write(self.srv.get()) - -app = pywss.App() -app.view("/user", UserView) # 注册视图路由->自动注入依赖 -app.run() -``` - -### 强大的文件路由机制 -见 [文件路由](https://czasg.github.io/pywss/advance/file-route) diff --git a/pywss/__init__.py b/pywss/__init__.py index 5a78abc..db574e8 100644 --- a/pywss/__init__.py +++ b/pywss/__init__.py @@ -5,6 +5,7 @@ import time import gzip import queue +import signal import loggus import socket import inspect @@ -21,7 +22,6 @@ from pywss.handler import * from pywss.websocket import WebSocketUpgrade from pywss.testing import HttpTestRequest, HttpTestResponse -from pywss.closing import Closing from pywss.routing import Route from pywss.openapi import openapi_ui_template from pywss.utils import split_method_route, merge_dict @@ -760,12 +760,14 @@ def run( thread_pool_idle_time: int = int(os.environ.get("PYWSS_THREAD_POOL_IDLE_TIME", 300)), watch: bool = os.environ.get("PYWSS_WATCHDOG_ENABLE", "false").lower() == "true", ) -> None: - # global closing manager - Closing.add_close(self.close) # build app with [route:handler] self.build() # watchdog watch and threading.Thread(target=self.watchdog, daemon=True).start() + # rigister signal closing + for sig in (signal.SIGTERM, signal.SIGINT, signal.SIGILL): + signal.signal(sig, lambda *args: self.close()) + # socket with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.bind((host, port)) sock.listen(select_size) diff --git a/pywss/closing.py b/pywss/closing.py deleted file mode 100644 index 4d59e1e..0000000 --- a/pywss/closing.py +++ /dev/null @@ -1,20 +0,0 @@ -# coding: utf-8 -import signal - - -class Closing: - closes = [] - - @classmethod - def add_close(cls, close: callable): - cls.closes.append(close) - - @classmethod - def close(cls): - for close in cls.closes: - close() - - -signal.signal(signal.SIGTERM, lambda *args: Closing.close()) -signal.signal(signal.SIGINT, lambda *args: Closing.close()) -signal.signal(signal.SIGILL, lambda *args: Closing.close())