From 5dc0ffc6eb6ce672c2710e311b8bfaaa8b91adb6 Mon Sep 17 00:00:00 2001 From: Ciro Date: Mon, 14 Nov 2022 16:11:08 -0300 Subject: [PATCH] no more cork workaround is needed --- bench/socketify_plaintext.py | 6 ++++-- src/socketify/loop.py | 35 +++++++++++++++++++++++++---------- src/socketify/native/Makefile | 5 +++-- src/socketify/socketify.py | 12 +++--------- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/bench/socketify_plaintext.py b/bench/socketify_plaintext.py index d0313cc..06baf19 100644 --- a/bench/socketify_plaintext.py +++ b/bench/socketify_plaintext.py @@ -2,6 +2,8 @@ from socketify import App import os import multiprocessing + + def run_app(): app = App() app.get("/", lambda res, req: res.end("Hello, World!")) @@ -15,7 +17,7 @@ def create_fork(): run_app() # 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 :) \ No newline at end of file diff --git a/src/socketify/loop.py b/src/socketify/loop.py index 02708cc..1af87b3 100644 --- a/src/socketify/loop.py +++ b/src/socketify/loop.py @@ -2,10 +2,12 @@ import asyncio import threading import time -from queue import Queue from .uv import UVLoop +import asyncio +import uwebsocketspy + def future_handler(future, loop, exception_handler, response): try: @@ -28,7 +30,6 @@ class Loop: def __init__(self, exception_handler=None): self.loop = asyncio.new_event_loop() self.uv_loop = UVLoop() - self.queue = Queue() if hasattr(exception_handler, '__call__'): self.exception_handler = exception_handler @@ -43,8 +44,6 @@ class Loop: def set_timeout(self, timeout, callback, user_data): return self.uv_loop.create_timer(timeout, 0, callback, user_data) - def enqueue(self, callback, user_data): - self.queue.put((callback, user_data)) def create_future(self): return self.loop.create_future() @@ -53,11 +52,6 @@ class Loop: self.started = True #run asyncio once per tick def tick(loop): - #only call one item of the queue per tick - if not loop.queue.empty(): - (callback, user_data) = loop.queue.get(False) - callback(user_data) - loop.queue.task_done() #run once asyncio loop.run_once_asyncio() @@ -117,4 +111,25 @@ class Loop: # runner.run(main()) # else: # uvloop.install() -# asyncio.run(main()) \ No newline at end of file +# asyncio.run(main()) + + +#https://github.com/SpaceBlocks/uWebSockets.py/blob/master/src/Selector.h +# class UVSelector(asyncio.SelectorEventLoop): +# def tick(self): +# pass + +# # We expose our own event loop for use with asyncio +# class AsyncioUVLoop(asyncio.SelectorEventLoop): +# def __init__(self): +# self.selector = UVSelector() +# super().__init__(self.selector) +# def call_soon(self, *args, **kwargs): +# self.selector.tick() +# return super().call_soon(*args, **kwargs) +# def call_at(self, *args, **kwargs): +# self.selector.tick() +# return super().call_at(*args, **kwargs) + +# asyncio.set_event_loop(uws.Loop()) +# asyncio.get_event_loop().run_forever() diff --git a/src/socketify/native/Makefile b/src/socketify/native/Makefile index 798277d..27b76b4 100644 --- a/src/socketify/native/Makefile +++ b/src/socketify/native/Makefile @@ -67,5 +67,6 @@ linux: cd ../uWebSockets/uSockets && $(AR) rvs uSockets_linux_$(ARCH).a *.o # build CAPI + libsocketify - $(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp - $(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv \ No newline at end of file + $(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c --march=native and -Ofast ./src/$(LIBRARY_NAME).cpp + $(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv + diff --git a/src/socketify/socketify.py b/src/socketify/socketify.py index 7d46191..db793db 100644 --- a/src/socketify/socketify.py +++ b/src/socketify/socketify.py @@ -835,11 +835,8 @@ class WebSocket: def cork(self, callback): self._cork_handler = callback - if is_python: #call is enqueued to garantee corking works properly in python3 - self.loop.enqueue(lambda instance: lib.uws_ws_cork(instance.SSL, instance.ws, uws_ws_cork_handler, instance._ptr), self) - else: #just add to uvloop in next tick to garantee corking works properly in pypy3 - self.loop.set_timeout(0, lambda instance: lib.uws_ws_cork(instance.SSL, instance.ws, uws_ws_cork_handler, instance._ptr), self) - + lib.uws_ws_cork(self.SSL, self.ws, uws_ws_cork_handler, self._ptr) + def __del__(self): #free SocketRefs when if needed if self.free_socket_data: @@ -1068,10 +1065,7 @@ class AppResponse: if not self.aborted: self.grab_aborted_handler() self._cork_handler = callback - if is_python: #call is enqueued to garantee corking works properly in python3 - self.loop.enqueue(lambda instance: lib.uws_res_cork(instance.SSL, instance.res, uws_generic_cork_handler, instance._ptr), self) - else: #just add to uvloop in next tick to garantee corking works properly in pypy3 - self.loop.set_timeout(0, lambda instance: lib.uws_res_cork(instance.SSL, instance.res, uws_generic_cork_handler, instance._ptr), self) + lib.uws_res_cork(self.SSL, self.res, uws_generic_cork_handler, self._ptr) def set_cookie(self, name, value, options={}):