-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Drop BackgroundManager in favor of fork(func1, func2) #603
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,12 @@ | ||
| import functools | ||
| import ssl | ||
| import typing | ||
| from types import TracebackType | ||
|
|
||
| import trio | ||
|
|
||
| from ..config import PoolLimits, Timeout | ||
| from ..exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout | ||
| from .base import ( | ||
| BaseBackgroundManager, | ||
| BaseEvent, | ||
| BasePoolSemaphore, | ||
| BaseSocketStream, | ||
|
|
@@ -204,17 +202,31 @@ def run( | |
| functools.partial(coroutine, **kwargs) if kwargs else coroutine, *args | ||
| ) | ||
|
|
||
| async def fork( | ||
| self, | ||
| coroutine1: typing.Callable, | ||
| args1: typing.Sequence, | ||
| coroutine2: typing.Callable, | ||
| args2: typing.Sequence, | ||
| ) -> None: | ||
| try: | ||
| async with trio.open_nursery() as nursery: | ||
| nursery.start_soon(coroutine1, *args1) | ||
| nursery.start_soon(coroutine2, *args2) | ||
| except trio.MultiError as exc: | ||
| # NOTE: asyncio doesn't handle multi-errors yet, so we must align on its | ||
| # behavior here, and need to arbitrarily decide which exception to raise. | ||
| # We may want to add an 'httpx.MultiError', manually add support | ||
| # for this situation in the asyncio backend, and re-raise | ||
| # an 'httpx.MultiError' from trio's here. | ||
|
Contributor
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. We can be more direct on our wording here. If |
||
| raise exc.exceptions[0] | ||
|
|
||
| def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore: | ||
| return PoolSemaphore(limits) | ||
|
|
||
| def create_event(self) -> BaseEvent: | ||
| return Event() | ||
|
|
||
| def background_manager( | ||
| self, coroutine: typing.Callable, *args: typing.Any | ||
| ) -> "BackgroundManager": | ||
| return BackgroundManager(coroutine, *args) | ||
|
|
||
|
|
||
| class Event(BaseEvent): | ||
| def __init__(self) -> None: | ||
|
|
@@ -233,25 +245,3 @@ def clear(self) -> None: | |
| # trio.Event.clear() was deprecated in Trio 0.12. | ||
| # https://github.com/python-trio/trio/issues/637 | ||
| self._event = trio.Event() | ||
|
|
||
|
|
||
| class BackgroundManager(BaseBackgroundManager): | ||
| def __init__(self, coroutine: typing.Callable, *args: typing.Any) -> None: | ||
| self.coroutine = coroutine | ||
| self.args = args | ||
| self.nursery_manager = trio.open_nursery() | ||
| self.nursery: typing.Optional[trio.Nursery] = None | ||
|
|
||
| async def __aenter__(self) -> "BackgroundManager": | ||
| self.nursery = await self.nursery_manager.__aenter__() | ||
| self.nursery.start_soon(self.coroutine, *self.args) | ||
| return self | ||
|
|
||
| async def __aexit__( | ||
| self, | ||
| exc_type: typing.Type[BaseException] = None, | ||
| exc_value: BaseException = None, | ||
| traceback: TracebackType = None, | ||
| ) -> None: | ||
| assert self.nursery is not None | ||
| await self.nursery_manager.__aexit__(exc_type, exc_value, traceback) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This'll look a bunch nicer with
anyio. (Tho we should leave Trio's implementation alone, since they already have a sensible nursery primitive.)