77import ssl as __ssl
88import sys as __sys
99import typing as __typing
10- import asyncio .base_events as __asyncio_base_events
11- import io as __io
10+
11+ from ._loop import PyLoop as Loop
12+ from ._loop import __version__
13+ from ._loop import new_event_loop
14+ from ._loop import start_server as __start_server
15+ from ._loop import open_connection as __open_connection
16+ from ._loop import profiler_running
17+ from ._loop import start_profiler
18+ from ._loop import stop_profiler
1219
1320__DLL_DIR_HANDLES : list [object ] = []
1421
@@ -49,14 +56,6 @@ def __configure_windows_dll_search_path() -> None:
4956
5057__configure_windows_dll_search_path ()
5158
52- from ._loop import PyLoop as Loop
53- from ._loop import __version__
54- from ._loop import open_connection as __open_connection
55- from ._loop import profiler_running as __profiler_running
56- from ._loop import start_profiler as __start_profiler
57- from ._loop import start_server as __start_server
58- from ._loop import stop_profiler as __stop_profiler
59-
6059__all__ : tuple [str , ...] = (
6160 "Loop" ,
6261 "__version__" ,
@@ -102,24 +101,6 @@ def __set_event_loop(loop: Loop | None) -> None:
102101 __asyncio .events .set_event_loop = __set_event_loop
103102
104103
105- def new_event_loop () -> Loop :
106- return Loop ()
107-
108-
109- def profiler_running () -> bool :
110- return __profiler_running ()
111-
112-
113- def start_profiler () -> None :
114- """Start a Tracy profiling session."""
115- __start_profiler ()
116-
117-
118- def stop_profiler () -> None :
119- """Stop the active Tracy profiling session."""
120- __stop_profiler ()
121-
122-
123104@__contextlib .contextmanager
124105def profile () -> __typing .Iterator [None ]:
125106 """Context manager wrapper around ``start_profiler()`` / ``stop_profiler()``."""
@@ -141,130 +122,17 @@ def profile() -> __typing.Iterator[None]:
141122__ORIG_SOCK_RECVFROM_INTO = getattr (Loop , "sock_recvfrom_into" , None )
142123__ORIG_SOCK_SENDTO = getattr (Loop , "sock_sendto" , None )
143124__ORIG_SOCK_SENDFILE = getattr (Loop , "sock_sendfile" , None )
144- __ORIG_RUN_FOREVER = Loop .run_forever
145- __ORIG_RUN_UNTIL_COMPLETE = Loop .run_until_complete
146- __ORIG_SHUTDOWN_ASYNCGENS = Loop .shutdown_asyncgens
147- __ORIG_CLOSE = Loop .close
148- __ORIG_CREATE_TASK = Loop .create_task
149125__USE_FAST_STREAMS = __os .environ .get ("RSLOOP_USE_FAST_STREAMS" , "1" ) != "0"
150- __ASYNCGEN_STATE : dict [Loop , dict [str , object ]] = {}
151- __LOOP_CONFIG : dict [Loop , dict [str , object ]] = {}
152126
153127if __USE_FAST_STREAMS and __asyncio .open_connection is __ORIG_OPEN_CONNECTION :
154128 __asyncio .open_connection = __open_connection
155129if __USE_FAST_STREAMS and __asyncio .start_server is __ORIG_START_SERVER :
156130 __asyncio .start_server = __start_server
157131
158132_asyncio = __asyncio
159- _io = __io
160133_os = __os
161134
162135
163- def __get_asyncgen_state (loop : Loop ) -> dict [str , object ]:
164- state = __ASYNCGEN_STATE .get (loop )
165- if state is None :
166- state = {
167- "active" : set (),
168- "shutdown_called" : False ,
169- "old_hooks" : None ,
170- }
171- __ASYNCGEN_STATE [loop ] = state
172- return state
173-
174-
175- def __get_loop_config (loop : Loop ) -> dict [str , object ]:
176- config = __LOOP_CONFIG .get (loop )
177- if config is None :
178- config = {
179- "slow_callback_duration" : 0.1 ,
180- }
181- __LOOP_CONFIG [loop ] = config
182- return config
183-
184-
185- @__contextlib .contextmanager
186- def __asyncgen_hooks_installed (loop : Loop ) -> __typing .Iterator [None ]:
187- state = __get_asyncgen_state (loop )
188- old_hooks = __sys .get_asyncgen_hooks ()
189- state ["old_hooks" ] = old_hooks
190- __sys .set_asyncgen_hooks (
191- firstiter = lambda agen : __asyncgen_firstiter_hook (loop , agen ),
192- finalizer = lambda agen : __asyncgen_finalizer_hook (loop , agen ),
193- )
194- try :
195- yield None
196- finally :
197- saved_hooks = state .get ("old_hooks" )
198- if saved_hooks is not None :
199- __sys .set_asyncgen_hooks (* saved_hooks )
200- state ["old_hooks" ] = None
201-
202-
203- def __asyncgen_firstiter_hook (loop : Loop , agen ) -> None :
204- state = __get_asyncgen_state (loop )
205- if state ["shutdown_called" ]:
206- import warnings as __warnings
207-
208- __warnings .warn (
209- f"asynchronous generator { agen !r} was scheduled after "
210- f"loop.shutdown_asyncgens() call" ,
211- ResourceWarning ,
212- source = loop ,
213- )
214- state ["active" ].add (agen )
215-
216-
217- def __asyncgen_finalizer_hook (loop : Loop , agen ) -> None :
218- state = __get_asyncgen_state (loop )
219- state ["active" ].discard (agen )
220- if not loop .is_closed ():
221- loop .call_soon_threadsafe (loop .create_task , agen .aclose ())
222-
223-
224- async def __loop_shutdown_asyncgens (self ):
225- state = __get_asyncgen_state (self )
226- state ["shutdown_called" ] = True
227-
228- if not state ["active" ]:
229- return
230-
231- closing_agens = list (state ["active" ])
232- state ["active" ].clear ()
233-
234- results = await __asyncio .gather (
235- * (agen .aclose () for agen in closing_agens ),
236- return_exceptions = True ,
237- )
238-
239- for result , agen in zip (results , closing_agens ):
240- if isinstance (result , Exception ):
241- self .call_exception_handler (
242- {
243- "message" : f"an error occurred during closing of asynchronous generator { agen !r} " ,
244- "exception" : result ,
245- "asyncgen" : agen ,
246- }
247- )
248-
249-
250- def __loop_run_forever (self ):
251- with __asyncgen_hooks_installed (self ):
252- return __ORIG_RUN_FOREVER (self )
253-
254-
255- def __loop_run_until_complete (self , future ):
256- with __asyncgen_hooks_installed (self ):
257- return __ORIG_RUN_UNTIL_COMPLETE (self , future )
258-
259-
260- def __loop_close (self ):
261- try :
262- return __ORIG_CLOSE (self )
263- finally :
264- __ASYNCGEN_STATE .pop (self , None )
265- __LOOP_CONFIG .pop (self , None )
266-
267-
268136def __cancel_all_tasks (loop : Loop ) -> None :
269137 to_cancel = __asyncio .all_tasks (loop )
270138 if not to_cancel :
@@ -574,14 +442,6 @@ async def __loop_sock_sendfile(
574442 file .seek (offset + total_sent )
575443
576444
577- def __get_slow_callback_duration (self ):
578- return __get_loop_config (self )["slow_callback_duration" ]
579-
580-
581- def __set_slow_callback_duration (self , value ):
582- __get_loop_config (self )["slow_callback_duration" ] = float (value )
583-
584-
585445async def __loop_create_datagram_endpoint (
586446 self ,
587447 protocol_factory ,
@@ -1522,24 +1382,6 @@ async def __loop_create_connection(
15221382if __ORIG_SOCK_SENDFILE is None :
15231383 Loop .sock_sendfile = __loop_sock_sendfile
15241384
1525- if not hasattr (Loop , "slow_callback_duration" ):
1526- Loop .slow_callback_duration = property (
1527- __get_slow_callback_duration ,
1528- __set_slow_callback_duration ,
1529- )
1530-
1531- if Loop .run_forever is __ORIG_RUN_FOREVER :
1532- Loop .run_forever = __loop_run_forever
1533-
1534- if Loop .run_until_complete is __ORIG_RUN_UNTIL_COMPLETE :
1535- Loop .run_until_complete = __loop_run_until_complete
1536-
1537- if Loop .shutdown_asyncgens is __ORIG_SHUTDOWN_ASYNCGENS :
1538- Loop .shutdown_asyncgens = __loop_shutdown_asyncgens
1539-
1540- if Loop .close is __ORIG_CLOSE :
1541- Loop .close = __loop_close
1542-
15431385# Keep the Rust implementation on the hot path. It already handles task
15441386# factories and keyword forwarding, while the Python wrapper adds measurable
15451387# overhead in task-heavy workloads.
0 commit comments