From e0e28263f50eb12b529cb578ad3bdf1308c8f0f8 Mon Sep 17 00:00:00 2001 From: Ciro Date: Tue, 14 Mar 2023 15:31:40 -0300 Subject: [PATCH] more WSGI stuff --- bench/asgi_wsgi/raw-wsgi.py | 28 ++++++++++++++++++++++++++-- bench/socketify_plaintext.py | 4 ++-- examples/static_files.py | 4 ++-- src/socketify/helpers.py | 2 +- src/socketify/uWebSockets | 2 +- src/socketify/wsgi.py | 5 ++++- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/bench/asgi_wsgi/raw-wsgi.py b/bench/asgi_wsgi/raw-wsgi.py index acf846a..738226e 100644 --- a/bench/asgi_wsgi/raw-wsgi.py +++ b/bench/asgi_wsgi/raw-wsgi.py @@ -30,7 +30,31 @@ def app_hello(environ, start_response): yield b'Hello, World!' if __name__ == "__main__": - from socketify import WSGI - WSGI(app_chunked).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(8) # import fastwsgi # fastwsgi.run(wsgi_app=app_hello, host='127.0.0.1', port=8000) + # from meinheld import server + # server.listen(("0.0.0.0", 8000)) + # server.run(app_hello) + from socketify import WSGI + WSGI(app_hello).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(1) + # def run_app(): + # import fastwsgi + # fastwsgi.run(wsgi_app=app_hello, host='127.0.0.1', port=8000) + + # import os + # pid_list = [] + # # fork limiting the cpu count - 1 + # for _ in range(1, 8): + # pid = os.fork() + # # n greater than 0 means parent process + # if not pid > 0: + # run_app() + # break + # pid_list.append(pid) + + # run_app() # run app on the main process too :) + + # # sigint everything to gracefull shutdown + # import signal + # for pid in pid_list: + # os.kill(pid, signal.SIGINT) diff --git a/bench/socketify_plaintext.py b/bench/socketify_plaintext.py index be15f43..320e49a 100644 --- a/bench/socketify_plaintext.py +++ b/bench/socketify_plaintext.py @@ -29,7 +29,7 @@ def create_fork(): # fork limiting the cpu count - 1 -for i in range(1, multiprocessing.cpu_count()): - create_fork() +# for i in range(1, multiprocessing.cpu_count()): +# create_fork() run_app() # run app on the main process too :) diff --git a/examples/static_files.py b/examples/static_files.py index 6536d64..19b77cc 100644 --- a/examples/static_files.py +++ b/examples/static_files.py @@ -6,8 +6,8 @@ # using oha -c 400 -z 5s http://localhost:3000/ # nginx - try_files - 77630.15 req/s -# pypy3 - socketify static - 15839.22 req/s -# python3 - socketify static - 8294.96 req/s +# pypy3 - socketify static - 16797.30 req/s +# python3 - socketify static - 10140.19 req/s # node.js - @fastify/static - 5437.16 req/s # node.js - express.static - 4077.49 req/s # python3 - socketify static_aiofile - 2390.96 req/s diff --git a/src/socketify/helpers.py b/src/socketify/helpers.py index eff0a8f..585d53c 100644 --- a/src/socketify/helpers.py +++ b/src/socketify/helpers.py @@ -222,7 +222,7 @@ def async_middleware(*functions): class DecoratorRouter: def __init__(self, app, prefix: str = "", *middlewares): self.app = app - self.middlewares = list(middlewares) + self.middlewares = list(*middlewares) self.prefix = prefix def get(self, path): diff --git a/src/socketify/uWebSockets b/src/socketify/uWebSockets index f5a4235..216c8b9 160000 --- a/src/socketify/uWebSockets +++ b/src/socketify/uWebSockets @@ -1 +1 @@ -Subproject commit f5a4235d2751c07d3f834ad86f42c2cab59eded0 +Subproject commit 216c8b9ea20ec8719175b136cf86cb51b316382d diff --git a/src/socketify/wsgi.py b/src/socketify/wsgi.py index 25b3a63..b233059 100644 --- a/src/socketify/wsgi.py +++ b/src/socketify/wsgi.py @@ -323,6 +323,7 @@ def wsgi(ssl, response, info, user_data, aborted): # no content-length if content_length < 0: is_chunked = True + content_length = ffi.cast("uintmax_t", content_length) def start_response(status, headers, exc_info=None): nonlocal headers_set, status_text @@ -355,7 +356,7 @@ def wsgi(ssl, response, info, user_data, aborted): return write failed_chunks = None - content_length = ffi.cast("uintmax_t", content_length) + last_offset = -1 data_retry = None # check for body @@ -379,6 +380,8 @@ def wsgi(ssl, response, info, user_data, aborted): if data: if not headers_written: write_headers(headers_set) + content_length = ffi.cast("uintmax_t", content_length) + if is_chunked: if isinstance(data, bytes): lib.uws_res_write(ssl, response, data, len(data))