Skip to content

Commit

Permalink
Remove nest-asyncio dep
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Feb 1, 2025
1 parent 893dbae commit c4862ef
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 17 deletions.
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ requires-python = '>=3.10'
dependencies = [
'kiwipy[rmq]~=0.8.5',
'msgpack~=1.1',
'nest_asyncio~=1.5,>=1.5.1',
'pyyaml~=6.0',
'typing-extensions~=4.12'
]
Expand Down Expand Up @@ -133,7 +132,6 @@ module = [
'frozendict.*',
'kiwipy.*',
'msgpack.*',
'nest_asyncio.*',
'tblib.*',
]
ignore_missing_imports = true
Expand Down
10 changes: 1 addition & 9 deletions src/plumpy/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ def new_event_loop(*args: Any, **kwargs: Any) -> asyncio.AbstractEventLoop:


class PlumpyEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
"""Custom event policy that always returns the same event loop that is made reentrant by ``nest_asyncio``."""
"""Custom event policy that avoid using deprecated asyncio.get_event_loop"""

_loop: asyncio.AbstractEventLoop | None = None

def new_event_loop(self) -> asyncio.AbstractEventLoop:
"""Create new event loop and patch as re-entrant loop."""
import nest_asyncio

self._loop = super().new_event_loop()
nest_asyncio.apply(self._loop)

return self._loop

Expand All @@ -52,11 +49,6 @@ def reset_event_loop_policy() -> None:
old_loop = asyncio.get_event_loop()
if not old_loop.is_closed():
# purge weakref to prevent memory leak
cls = old_loop.__class__

del cls._check_running # type: ignore
del cls._nest_patched # type: ignore

old_loop.close()

# 2. Reset the policy to the default (i.e. None):
Expand Down
3 changes: 1 addition & 2 deletions tests/notebooks/get_event_loop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"from plumpy import PlumpyEventLoopPolicy, set_event_loop_policy\n",
"\n",
"set_event_loop_policy()\n",
"assert isinstance(asyncio.get_event_loop_policy(), PlumpyEventLoopPolicy)\n",
"assert hasattr(asyncio.get_event_loop(), '_nest_patched')"
"assert isinstance(asyncio.get_event_loop_policy(), PlumpyEventLoopPolicy)\n"
]
}
],
Expand Down
2 changes: 0 additions & 2 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def test_get_event_loop():
set_event_loop_policy()
assert isinstance(asyncio.get_event_loop_policy(), PlumpyEventLoopPolicy)
assert asyncio.get_event_loop() is asyncio.get_event_loop()
assert asyncio.get_event_loop()._nest_patched is not None


def test_set_event_loop():
"""Test the ``set_event_loop`` raises ``NotImplementedError``."""
Expand Down
2 changes: 0 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c4862ef

Please sign in to comment.