Drop 'fork' (#619)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 83fc092 · Browse files · 5 files·+1 added−113 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+1 added−113 removed
- tests:+1 added−48 removed
- other:+0 added−65 removed
Provenance
- Commit
eba29da- Tree
c6f0d31- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
83fc092
5 files changed+1 added−113 removed
modified:httpx/concurrency/asyncio.py-22 removed
266 unmodified lines267 return self.loop.run_until_complete(coroutine(*args, **kwargs))268 finally:269 self._loop = loop270 271- async def fork(272- self,273- coroutine1: typing.Callable,274- args1: typing.Sequence,275- coroutine2: typing.Callable,276- args2: typing.Sequence,277- ) -> None:278- task1 = self.loop.create_task(coroutine1(*args1))279- task2 = self.loop.create_task(coroutine2(*args2))280-281- try:282- await asyncio.gather(task1, task2)283- finally:284- pending: typing.Set[asyncio.Future[typing.Any]] # Please mypy.285- _, pending = await asyncio.wait({task1, task2}, timeout=0)286- for task in pending:287- task.cancel()288- try:289- await task290- except asyncio.CancelledError:291- pass292-271 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:272 return PoolSemaphore(limits)273 274 def create_event(self) -> BaseEvent:1 unmodified linemodified:httpx/concurrency/auto.py-9 removed
49 unmodified lines50 return await self.backend.run_in_threadpool(func, *args, **kwargs)51 52 def create_event(self) -> BaseEvent:53 return self.backend.create_event()54-55- async def fork(56- self,57- coroutine1: typing.Callable,58- args1: typing.Sequence,59- coroutine2: typing.Callable,60- args2: typing.Sequence,61- ) -> None:62- return await self.backend.fork(coroutine1, args1, coroutine2, args2)modified:httpx/concurrency/base.py-18 removed
118 unmodified lines119 raise NotImplementedError() # pragma: no cover120 121 def create_event(self) -> BaseEvent:122 raise NotImplementedError() # pragma: no cover123-124- async def fork(125- self,126- coroutine1: typing.Callable,127- args1: typing.Sequence,128- coroutine2: typing.Callable,129- args2: typing.Sequence,130- ) -> None:131- """132- Run two coroutines concurrently.133-134- This should start 'coroutine1' with '*args1' and 'coroutine2' with '*args2',135- and wait for them to finish.136-137- In case one of the coroutines raises an exception, cancel the other one then138- raise. If the other coroutine had also raised an exception, ignore it.139- """140- raise NotImplementedError() # pragma: no covermodified:httpx/concurrency/trio.py-16 removed
166 unmodified lines167 return trio.run(168 functools.partial(coroutine, **kwargs) if kwargs else coroutine, *args169 )170 171- async def fork(172- self,173- coroutine1: typing.Callable,174- args1: typing.Sequence,175- coroutine2: typing.Callable,176- args2: typing.Sequence,177- ) -> None:178- try:179- async with trio.open_nursery() as nursery:180- nursery.start_soon(coroutine1, *args1)181- nursery.start_soon(coroutine2, *args2)182- except trio.MultiError as exc:183- # In practice, we don't actually care about raising both184- # exceptions, so let's raise either indeterminantly.185- raise exc.exceptions[0]186-171 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:172 return PoolSemaphore(limits)173 174 def create_event(self) -> BaseEvent:20 unmodified linesmodified:tests/test_concurrency.py+1 added-48 removed
6 unmodified lines7 from httpx.concurrency.asyncio import AsyncioBackend8 from httpx.concurrency.base import lookup_backend9 from httpx.concurrency.trio import TrioBackend10 from httpx.config import SSLConfig11-from tests.concurrency import get_cipher, run_concurrently, sleep11+from tests.concurrency import get_cipher, run_concurrently12 13 14 async def read_response(stream, timeout: Timeout, should_contain: bytes) -> bytes:15 # stream.read() only gives us *up to* as much data as we ask for. In order to82 unmodified lines98 finally:99 await stream.close()100 101 102-async def test_fork(backend):103- backend = lookup_backend(backend)104- ok_counter = 0105-106- async def ok(delay: int) -> None:107- nonlocal ok_counter108- await sleep(backend, delay)109- ok_counter += 1110-111- async def fail(message: str, delay: int) -> None:112- await sleep(backend, delay)113- raise RuntimeError(message)114-115- await backend.fork(ok, [0], ok, [0])116- assert ok_counter == 2117-118- with pytest.raises(RuntimeError, match="Oops"):119- await backend.fork(ok, [0], fail, ["Oops", 0.01])120-121- assert ok_counter == 3122-123- with pytest.raises(RuntimeError, match="Oops"):124- await backend.fork(ok, [0.01], fail, ["Oops", 0])125-126- assert ok_counter == 3127-128- with pytest.raises(RuntimeError, match="Oops"):129- await backend.fork(fail, ["Oops", 0.01], ok, [0])130-131- assert ok_counter == 4132-133- with pytest.raises(RuntimeError, match="Oops"):134- await backend.fork(fail, ["Oops", 0], ok, [0.01])135-136- assert ok_counter == 4137-138- with pytest.raises(RuntimeError, match="My bad"):139- await backend.fork(fail, ["My bad", 0], fail, ["Oops", 0.01])140-141- with pytest.raises(RuntimeError, match="Oops"):142- await backend.fork(fail, ["My bad", 0.01], fail, ["Oops", 0])143-144- # No 'match', since we can't know which will be raised first.145- with pytest.raises(RuntimeError):146- await backend.fork(fail, ["My bad", 0], fail, ["Oops", 0])147-148-102 def test_lookup_backend():103 assert isinstance(lookup_backend("asyncio"), AsyncioBackend)104 assert isinstance(lookup_backend("trio"), TrioBackend)105 assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)14 unmodified lines