kopia lustrzana https://github.com/cirospaciari/socketify.py
set running event loop
rodzic
e681d628b7
commit
09552ac20c
|
@ -1,5 +1,6 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
from .uv import UVLoop
|
from .uv import UVLoop
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
@ -52,11 +53,19 @@ class Loop:
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.started = True
|
self.started = True
|
||||||
|
try:
|
||||||
|
asyncio.events._set_running_loop(self.loop)
|
||||||
|
self.loop._thread_id = threading.get_ident()
|
||||||
while self.started:
|
while self.started:
|
||||||
self.run_once_asyncio()
|
# run one step of asyncio
|
||||||
|
self.loop._stopping = True
|
||||||
|
self.loop._run_once()
|
||||||
|
# run one step of libuv
|
||||||
self.uv_loop.run_once()
|
self.uv_loop.run_once()
|
||||||
|
finally:
|
||||||
# Find all running tasks in main thread:
|
self.loop._thread_id = None
|
||||||
|
asyncio.events._set_running_loop(None)
|
||||||
|
# Find all running tasks in main thread
|
||||||
pending = asyncio.all_tasks(self.loop)
|
pending = asyncio.all_tasks(self.loop)
|
||||||
# Run loop until tasks done
|
# Run loop until tasks done
|
||||||
self.loop.run_until_complete(asyncio.gather(*pending))
|
self.loop.run_until_complete(asyncio.gather(*pending))
|
||||||
|
@ -64,12 +73,12 @@ class Loop:
|
||||||
self.uv_loop.stop()
|
self.uv_loop.stop()
|
||||||
|
|
||||||
def run_once(self):
|
def run_once(self):
|
||||||
self.uv_loop.run_once()
|
# run one step of asyncio
|
||||||
|
|
||||||
def run_once_asyncio(self):
|
|
||||||
# run only one step
|
|
||||||
self.loop._stopping = True
|
self.loop._stopping = True
|
||||||
self.loop._run_once()
|
self.loop._run_once()
|
||||||
|
# run one step of libuv
|
||||||
|
self.uv_loop.run_once()
|
||||||
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
# Just mark as started = False and wait
|
# Just mark as started = False and wait
|
||||||
|
@ -88,10 +97,9 @@ class Loop:
|
||||||
lambda f: future_handler(f, self.loop, self.exception_handler, response)
|
lambda f: future_handler(f, self.loop, self.exception_handler, response)
|
||||||
)
|
)
|
||||||
# force asyncio run once to enable req in async functions before first await
|
# force asyncio run once to enable req in async functions before first await
|
||||||
self.run_once_asyncio()
|
self.loop._stopping = True
|
||||||
|
self.loop._run_once()
|
||||||
|
|
||||||
# if response != None: #set auto cork
|
|
||||||
# response.needs_cork = True
|
|
||||||
return future
|
return future
|
||||||
|
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue