kopia lustrzana https://github.com/cirospaciari/socketify.py
no more cork workaround is needed
rodzic
8ae98364c0
commit
5dc0ffc6eb
|
@ -2,6 +2,8 @@ from socketify import App
|
||||||
import os
|
import os
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def run_app():
|
def run_app():
|
||||||
app = App()
|
app = App()
|
||||||
app.get("/", lambda res, req: res.end("Hello, World!"))
|
app.get("/", lambda res, req: res.end("Hello, World!"))
|
||||||
|
@ -15,7 +17,7 @@ def create_fork():
|
||||||
run_app()
|
run_app()
|
||||||
|
|
||||||
# fork limiting the cpu count - 1
|
# fork limiting the cpu count - 1
|
||||||
for i in range(1, multiprocessing.cpu_count()):
|
# for i in range(1, multiprocessing.cpu_count()):
|
||||||
create_fork()
|
# create_fork()
|
||||||
|
|
||||||
run_app() # run app on the main process too :)
|
run_app() # run app on the main process too :)
|
|
@ -2,10 +2,12 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from queue import Queue
|
|
||||||
|
|
||||||
from .uv import UVLoop
|
from .uv import UVLoop
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import uwebsocketspy
|
||||||
|
|
||||||
|
|
||||||
def future_handler(future, loop, exception_handler, response):
|
def future_handler(future, loop, exception_handler, response):
|
||||||
try:
|
try:
|
||||||
|
@ -28,7 +30,6 @@ class Loop:
|
||||||
def __init__(self, exception_handler=None):
|
def __init__(self, exception_handler=None):
|
||||||
self.loop = asyncio.new_event_loop()
|
self.loop = asyncio.new_event_loop()
|
||||||
self.uv_loop = UVLoop()
|
self.uv_loop = UVLoop()
|
||||||
self.queue = Queue()
|
|
||||||
|
|
||||||
if hasattr(exception_handler, '__call__'):
|
if hasattr(exception_handler, '__call__'):
|
||||||
self.exception_handler = exception_handler
|
self.exception_handler = exception_handler
|
||||||
|
@ -43,8 +44,6 @@ class Loop:
|
||||||
def set_timeout(self, timeout, callback, user_data):
|
def set_timeout(self, timeout, callback, user_data):
|
||||||
return self.uv_loop.create_timer(timeout, 0, 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):
|
def create_future(self):
|
||||||
return self.loop.create_future()
|
return self.loop.create_future()
|
||||||
|
@ -53,11 +52,6 @@ class Loop:
|
||||||
self.started = True
|
self.started = True
|
||||||
#run asyncio once per tick
|
#run asyncio once per tick
|
||||||
def tick(loop):
|
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
|
#run once asyncio
|
||||||
loop.run_once_asyncio()
|
loop.run_once_asyncio()
|
||||||
|
|
||||||
|
@ -117,4 +111,25 @@ class Loop:
|
||||||
# runner.run(main())
|
# runner.run(main())
|
||||||
# else:
|
# else:
|
||||||
# uvloop.install()
|
# uvloop.install()
|
||||||
# asyncio.run(main())
|
# 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()
|
||||||
|
|
|
@ -67,5 +67,6 @@ linux:
|
||||||
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_linux_$(ARCH).a *.o
|
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_linux_$(ARCH).a *.o
|
||||||
|
|
||||||
# build CAPI + libsocketify
|
# 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) -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
|
$(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
|
||||||
|
|
||||||
|
|
|
@ -835,11 +835,8 @@ class WebSocket:
|
||||||
|
|
||||||
def cork(self, callback):
|
def cork(self, callback):
|
||||||
self._cork_handler = callback
|
self._cork_handler = callback
|
||||||
if is_python: #call is enqueued to garantee corking works properly in python3
|
lib.uws_ws_cork(self.SSL, self.ws, uws_ws_cork_handler, self._ptr)
|
||||||
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)
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
#free SocketRefs when if needed
|
#free SocketRefs when if needed
|
||||||
if self.free_socket_data:
|
if self.free_socket_data:
|
||||||
|
@ -1068,10 +1065,7 @@ class AppResponse:
|
||||||
if not self.aborted:
|
if not self.aborted:
|
||||||
self.grab_aborted_handler()
|
self.grab_aborted_handler()
|
||||||
self._cork_handler = callback
|
self._cork_handler = callback
|
||||||
if is_python: #call is enqueued to garantee corking works properly in python3
|
lib.uws_res_cork(self.SSL, self.res, uws_generic_cork_handler, self._ptr)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def set_cookie(self, name, value, options={}):
|
def set_cookie(self, name, value, options={}):
|
||||||
|
|
Ładowanie…
Reference in New Issue