-
Notifications
You must be signed in to change notification settings - Fork 566
restore uvloop.new_event_loop and other missing uvloop members to typing #573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
__all__ = ('new_event_loop', 'install', 'EventLoopPolicy') | ||
|
||
|
||
_T = _typing.TypeVar("_T") | ||
|
||
|
||
class Loop(__BaseLoop, __asyncio.AbstractEventLoop): # type: ignore[misc] | ||
pass | ||
|
||
|
@@ -34,69 +37,84 @@ def install() -> None: | |
__asyncio.set_event_loop_policy(EventLoopPolicy()) | ||
|
||
|
||
def run(main, *, loop_factory=new_event_loop, debug=None, **run_kwargs): | ||
"""The preferred way of running a coroutine with uvloop.""" | ||
|
||
async def wrapper(): | ||
# If `loop_factory` is provided we want it to return | ||
# either uvloop.Loop or a subtype of it, assuming the user | ||
# is using `uvloop.run()` intentionally. | ||
loop = __asyncio._get_running_loop() | ||
if not isinstance(loop, Loop): | ||
raise TypeError('uvloop.run() uses a non-uvloop event loop') | ||
return await main | ||
|
||
vi = _sys.version_info[:2] | ||
|
||
if vi <= (3, 10): | ||
# Copied from python/cpython | ||
|
||
if __asyncio._get_running_loop() is not None: | ||
raise RuntimeError( | ||
"asyncio.run() cannot be called from a running event loop") | ||
|
||
if not __asyncio.iscoroutine(main): | ||
raise ValueError("a coroutine was expected, got {!r}".format(main)) | ||
|
||
loop = loop_factory() | ||
try: | ||
__asyncio.set_event_loop(loop) | ||
if debug is not None: | ||
loop.set_debug(debug) | ||
return loop.run_until_complete(wrapper()) | ||
finally: | ||
if _typing.TYPE_CHECKING: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just inline the typing and omit this TYPE_CHECKING check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that, if you see the history of the PR, but it took too many changes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see now. What about adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that first also and it means the other members aren't type checked |
||
def run( | ||
main: _typing.Coroutine[_typing.Any, _typing.Any, _T], | ||
*, | ||
loop_factory: _typing.Optional[ | ||
_typing.Callable[[], Loop] | ||
] = new_event_loop, | ||
debug: _typing.Optional[bool]=None, | ||
) -> _T: | ||
"""The preferred way of running a coroutine with uvloop.""" | ||
else: | ||
def run(main, *, loop_factory=new_event_loop, debug=None, **run_kwargs): | ||
"""The preferred way of running a coroutine with uvloop.""" | ||
|
||
async def wrapper(): | ||
# If `loop_factory` is provided we want it to return | ||
# either uvloop.Loop or a subtype of it, assuming the user | ||
# is using `uvloop.run()` intentionally. | ||
loop = __asyncio._get_running_loop() | ||
if not isinstance(loop, Loop): | ||
raise TypeError('uvloop.run() uses a non-uvloop event loop') | ||
return await main | ||
|
||
vi = _sys.version_info[:2] | ||
|
||
if vi <= (3, 10): | ||
# Copied from python/cpython | ||
|
||
if __asyncio._get_running_loop() is not None: | ||
raise RuntimeError( | ||
"asyncio.run() cannot be called from a running event loop") | ||
|
||
if not __asyncio.iscoroutine(main): | ||
raise ValueError( | ||
"a coroutine was expected, got {!r}".format(main) | ||
) | ||
|
||
loop = loop_factory() | ||
try: | ||
_cancel_all_tasks(loop) | ||
loop.run_until_complete(loop.shutdown_asyncgens()) | ||
if hasattr(loop, 'shutdown_default_executor'): | ||
loop.run_until_complete(loop.shutdown_default_executor()) | ||
__asyncio.set_event_loop(loop) | ||
if debug is not None: | ||
loop.set_debug(debug) | ||
return loop.run_until_complete(wrapper()) | ||
finally: | ||
__asyncio.set_event_loop(None) | ||
loop.close() | ||
|
||
elif vi == (3, 11): | ||
if __asyncio._get_running_loop() is not None: | ||
raise RuntimeError( | ||
"asyncio.run() cannot be called from a running event loop") | ||
|
||
with __asyncio.Runner( | ||
loop_factory=loop_factory, | ||
debug=debug, | ||
**run_kwargs | ||
) as runner: | ||
return runner.run(wrapper()) | ||
|
||
else: | ||
assert vi >= (3, 12) | ||
return __asyncio.run( | ||
wrapper(), | ||
loop_factory=loop_factory, | ||
debug=debug, | ||
**run_kwargs | ||
) | ||
|
||
|
||
def _cancel_all_tasks(loop): | ||
try: | ||
_cancel_all_tasks(loop) | ||
loop.run_until_complete(loop.shutdown_asyncgens()) | ||
if hasattr(loop, 'shutdown_default_executor'): | ||
loop.run_until_complete( | ||
loop.shutdown_default_executor() | ||
) | ||
finally: | ||
__asyncio.set_event_loop(None) | ||
loop.close() | ||
|
||
elif vi == (3, 11): | ||
if __asyncio._get_running_loop() is not None: | ||
raise RuntimeError( | ||
"asyncio.run() cannot be called from a running event loop") | ||
|
||
with __asyncio.Runner( | ||
loop_factory=loop_factory, | ||
debug=debug, | ||
**run_kwargs | ||
) as runner: | ||
return runner.run(wrapper()) | ||
|
||
else: | ||
assert vi >= (3, 12) | ||
return __asyncio.run( | ||
wrapper(), | ||
loop_factory=loop_factory, | ||
debug=debug, | ||
**run_kwargs | ||
) | ||
|
||
|
||
def _cancel_all_tasks(loop: __asyncio.AbstractEventLoop) -> None: | ||
# Copied from python/cpython | ||
|
||
to_cancel = __asyncio.all_tasks(loop) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding this file removed the useful members like 'new_event_loop' which is needed for |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.