mirror of
https://github.com/php-win-ext/grpc.git
synced 2026-03-24 17:12:19 +01:00
> [!IMPORTANT] > **This fix is only needed to support the use of grpc.aio outside of a running event loop.** This approach is [strongly discouraged](https://docs.python.org/3.14/library/asyncio-policy.html#asyncio-policies) by Python, and will be deprecated in future gRPC releases. > > Please use the [asyncio.run()](https://docs.python.org/3.14/library/asyncio-runner.html#asyncio.run) function or the [asyncio.Runner](https://docs.python.org/3.14/library/asyncio-runner.html#asyncio.Runner). If you see this in Python REPL, use the dedicated [asyncio REPL](https://docs.python.org/3/library/asyncio.html#asyncio-cli) by running `python -m asyncio`. This PR handles the following [asyncio behavioral changes](https://docs.python.org/3.14/whatsnew/3.14.html#id11) introduced in python3.14: - [asyncio.get_event_loop()](https://docs.python.org/3.14/library/asyncio-eventloop.html#asyncio.get_event_loop) now raises a `RuntimeError` if there is no current event loop, and no longer implicitly creates an event loop. - [Deprecations](https://docs.python.org/3.14/whatsnew/3.14.html#deprecated): [asyncio.get_event_loop_policy()](https://docs.python.org/3.14/library/asyncio-policy.html#asyncio.get_event_loop_policy), [asyncio.AbstractEventLoopPolicy](https://docs.python.org/3.14/library/asyncio-policy.html#asyncio.AbstractEventLoopPolicy), [asyncio.DefaultEventLoopPolicy](https://docs.python.org/3.14/library/asyncio-policy.html#asyncio.DefaultEventLoopPolicy). Note that this PR preserves the existing behavior and does not migrate off of [asyncio policy system](https://docs.python.org/3.14/library/asyncio-policy.html) yet. This will be done separately, see #39518. To support 3.14, this PR: 1. Fixes #39507 by handling the call to deprecated `asyncio.get_event_loop_policy()` when calling `new_event_loop()`. This was necessary because all warnings were elevated to errors within the context manager, and the newly deprecated policy caused an unhandled exception. 2. Handles the `BaseDefaultEventLoopPolicy.get_event_loop()` behavior change. [Before](https://github.com/python/cpython/blob/v3.13.7/Lib/asyncio/events.py#L695) python 3.14, it would only throw `RuntimeError` when there's no loop in non-main threads. [After](https://github.com/python/cpython/blob/v3.14.0rc3/Lib/asyncio/events.py#L714) python 3.14, it removes the special handling of the main thread. This PR preserves preserves the pre-3.14 grpc.aio behavior for 3.14. Closes #40750 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/40750 from sergiitk:fix/py/get_working_loop c4561debe5be3fda70177e060344f08fe0849852 PiperOrigin-RevId: 810291532