Skip to content

Commit 7962663

Browse files
committed
Add 2 new functions for using eventloops in non-deprecated ways
1 parent bd4551c commit 7962663

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pytest_asyncio/plugin.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,21 @@ def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[No
546546
_set_event_loop(old_loop)
547547

548548

549+
@contextlib.contextmanager
550+
def _temporary_event_loop(loop:AbstractEventLoop):
551+
try:
552+
old_event_loop = asyncio.get_event_loop()
553+
except RuntimeError:
554+
old_event_loop = None
555+
556+
asyncio.set_event_loop(old_event_loop)
557+
try:
558+
yield
559+
finally:
560+
asyncio.set_event_loop(old_event_loop)
561+
562+
563+
549564
def _get_event_loop_policy() -> AbstractEventLoopPolicy:
550565
with warnings.catch_warnings():
551566
warnings.simplefilter("ignore", DeprecationWarning)
@@ -772,6 +787,9 @@ def _scoped_runner(
772787
RuntimeWarning,
773788
)
774789

790+
791+
792+
775793
return _scoped_runner
776794

777795

@@ -780,6 +798,11 @@ def _scoped_runner(
780798
scope.value
781799
)
782800

801+
@pytest.fixture(scope="session", autouse=True)
802+
def new_event_loop() -> AbstractEventLoop:
803+
"""Creates a new eventloop for different tests being ran"""
804+
return asyncio.new_event_loop()
805+
783806

784807
@pytest.fixture(scope="session", autouse=True)
785808
def event_loop_policy() -> AbstractEventLoopPolicy:

0 commit comments

Comments
 (0)