Skip to content
Myr

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

5 files+1 added−113 removed

modified:httpx/concurrency/asyncio.py-22 removed
266 unmodified lines
267 return self.loop.run_until_complete(coroutine(*args, **kwargs))268 finally:269 self._loop = loop270271 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))280281 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 pass292271 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:272 return PoolSemaphore(limits)273274 def create_event(self) -> BaseEvent:
1 unmodified line
modified:httpx/concurrency/auto.py-9 removed
49 unmodified lines
50 return await self.backend.run_in_threadpool(func, *args, **kwargs)5152 def create_event(self) -> BaseEvent:53 return self.backend.create_event()5455 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 lines
119 raise NotImplementedError() # pragma: no cover120121 def create_event(self) -> BaseEvent:122 raise NotImplementedError() # pragma: no cover123124 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.133134 This should start 'coroutine1' with '*args1' and 'coroutine2' with '*args2',135 and wait for them to finish.136137 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 cover
modified:httpx/concurrency/trio.py-16 removed
166 unmodified lines
167 return trio.run(168 functools.partial(coroutine, **kwargs) if kwargs else coroutine, *args169 )170171 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]186171 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:172 return PoolSemaphore(limits)173174 def create_event(self) -> BaseEvent:
20 unmodified lines
modified:tests/test_concurrency.py+1 added-48 removed
6 unmodified lines
7from httpx.concurrency.asyncio import AsyncioBackend8from httpx.concurrency.base import lookup_backend9from httpx.concurrency.trio import TrioBackend10from httpx.config import SSLConfig11from tests.concurrency import get_cipher, run_concurrently, sleep11from tests.concurrency import get_cipher, run_concurrently121314async 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 to
82 unmodified lines
98 finally:99 await stream.close()100101102async def test_fork(backend):103 backend = lookup_backend(backend)104 ok_counter = 0105106 async def ok(delay: int) -> None:107 nonlocal ok_counter108 await sleep(backend, delay)109 ok_counter += 1110111 async def fail(message: str, delay: int) -> None:112 await sleep(backend, delay)113 raise RuntimeError(message)114115 await backend.fork(ok, [0], ok, [0])116 assert ok_counter == 2117118 with pytest.raises(RuntimeError, match="Oops"):119 await backend.fork(ok, [0], fail, ["Oops", 0.01])120121 assert ok_counter == 3122123 with pytest.raises(RuntimeError, match="Oops"):124 await backend.fork(ok, [0.01], fail, ["Oops", 0])125126 assert ok_counter == 3127128 with pytest.raises(RuntimeError, match="Oops"):129 await backend.fork(fail, ["Oops", 0.01], ok, [0])130131 assert ok_counter == 4132133 with pytest.raises(RuntimeError, match="Oops"):134 await backend.fork(fail, ["Oops", 0], ok, [0.01])135136 assert ok_counter == 4137138 with pytest.raises(RuntimeError, match="My bad"):139 await backend.fork(fail, ["My bad", 0], fail, ["Oops", 0.01])140141 with pytest.raises(RuntimeError, match="Oops"):142 await backend.fork(fail, ["My bad", 0.01], fail, ["Oops", 0])143144 # 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])147148102def 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