Skip to content
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

make BlockingPortal.stop sync #334

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
``FileWriteStream``so they accept any path-like object (including the new asynchronous ``Path``
class)
- Dropped unnecessary dependency on the ``async_generator`` library
- API changes:
* The following functions and methods are no longer asynchronous but can still be awaited on
(doing so will emit a deprecation warning):

* ``abc.BlockingPortal.stop``

**3.2.1**

Expand Down
12 changes: 10 additions & 2 deletions src/anyio/from_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from warnings import warn

from ._core import _eventloop
from ._core._compat import DeprecatedAwaitable
from ._core._eventloop import get_asynclib, get_cancelled_exc_class, threadlocals
from ._core._synchronization import Event
from ._core._tasks import CancelScope, create_task_group
Expand Down Expand Up @@ -130,7 +131,7 @@ async def __aenter__(self) -> 'BlockingPortal':
async def __aexit__(self, exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType]) -> Optional[bool]:
await self.stop()
self.stop()
return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)

def _check_running(self) -> None:
Expand All @@ -143,7 +144,7 @@ async def sleep_until_stopped(self) -> None:
"""Sleep until :meth:`stop` is called."""
await self._stop_event.wait()

async def stop(self, cancel_remaining: bool = False) -> None:
def stop(self, cancel_remaining: bool = False) -> DeprecatedAwaitable:
"""
Signal the portal to shut down.

Expand All @@ -154,11 +155,18 @@ async def stop(self, cancel_remaining: bool = False) -> None:
finish before returning

"""
if self._event_loop_thread_id is None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah this is wrong - we need to handle cancel_remaining

return
if threading.get_ident() != self._event_loop_thread_id:
raise RuntimeError('This method must be called from the event loop thread')

self._event_loop_thread_id = None
self._stop_event.set()
if cancel_remaining:
self._task_group.cancel_scope.cancel()

return DeprecatedAwaitable(self.stop)

async def _call_func(self, func: Callable, args: tuple, kwargs: Dict[str, Any],
future: Future) -> None:
def callback(f: Future) -> None:
Expand Down