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

port to anyio v3 #25

Merged
merged 3 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ flake8
isort==5.*
mypy
pytest
pytest-asyncio
pytest-cov
seed-isort-config
trio
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ known_third_party = anyio,pytest,setuptools
[tool:pytest]
addopts =
-rxXs
--anyio-backends=asyncio,trio
--cov=aiometer
--cov=tests
--cov-report=term-missing
--cov-fail-under=100
marks =
--strict-config
--strict-markers
filterwarnings=error
markers =
slow: Mark test as slow.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_long_description() -> str:
package_dir={"": "src"},
include_package_data=True,
zip_safe=False,
install_requires=["anyio==1.*", "typing-extensions==3.7.*; python_version<'3.8'"],
install_requires=["anyio~=3.2", "typing-extensions==3.7.*; python_version<'3.8'"],
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
python_requires=">=3.7",
license="MIT",
classifiers=[
Expand Down
1 change: 0 additions & 1 deletion src/aiometer/_concurrency/__init__.py

This file was deleted.

93 changes: 0 additions & 93 deletions src/aiometer/_concurrency/channels.py

This file was deleted.

25 changes: 12 additions & 13 deletions src/aiometer/_impl/amap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import anyio

from .._concurrency import open_memory_channel
from .run_on_each import run_on_each
from .types import T, U

Expand Down Expand Up @@ -60,26 +59,26 @@ def amap(
) -> AsyncContextManager[AsyncIterable]:
@asynccontextmanager
async def _amap() -> AsyncIterator[AsyncIterable]:
receive_channel, send_channel = open_memory_channel[Any](
send_channel, receive_channel = anyio.create_memory_object_stream(
max_buffer_size=len(args)
)

async with receive_channel, send_channel:
with send_channel, receive_channel:
async with anyio.create_task_group() as task_group:

async def sender() -> None:
await run_on_each(
async_fn,
args,
max_at_once=max_at_once,
max_per_second=max_per_second,
_include_index=_include_index,
_send_to=send_channel,
)
# Make any `async for ... in results: ...` terminate.
await send_channel.aclose()
with send_channel:
await run_on_each(
async_fn,
args,
max_at_once=max_at_once,
max_per_second=max_per_second,
_include_index=_include_index,
_send_to=send_channel,
)

await task_group.spawn(sender)
task_group.start_soon(sender)

yield receive_channel

Expand Down
6 changes: 3 additions & 3 deletions src/aiometer/_impl/meters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def new_state(self) -> MeterState:
class HardLimitMeter(Meter):
class State(MeterState):
def __init__(self, max_at_once: int) -> None:
self.semaphore = anyio.create_semaphore(max_at_once)
self.semaphore = anyio.Semaphore(max_at_once)

async def wait_task_can_start(self) -> None:
# anyio semaphore interface has no '.acquire()'.
Expand Down Expand Up @@ -60,7 +60,7 @@ async def wait_task_can_start(self) -> None:
# of the GCRA algorithm.
# `next_start_time` represents the TAT (theoretical time of arrival).
# See: https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm
now = await anyio.current_time()
now = anyio.current_time()
next_start_time = max(self.next_start_time, now)
time_until_start = next_start_time - now
threshold = self.period - self.task_delta
Expand All @@ -69,7 +69,7 @@ async def wait_task_can_start(self) -> None:
await anyio.sleep(max(0, time_until_start - threshold))

async def notify_task_started(self) -> None:
now = await anyio.current_time()
now = anyio.current_time()
self.next_start_time = max(self.next_start_time, now) + self.task_delta

async def notify_task_finished(self) -> None:
Expand Down
8 changes: 4 additions & 4 deletions src/aiometer/_impl/run_on_each.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Awaitable, Callable, List, NamedTuple, Optional, Sequence

import anyio
from anyio.streams.memory import MemoryObjectSendStream

from .._concurrency import MemorySendChannel
from .meters import HardLimitMeter, Meter, MeterState, RateLimitMeter
from .types import T


class _Config(NamedTuple):
include_index: bool
send_to: Optional[MemorySendChannel]
send_to: Optional[MemoryObjectSendStream]
meter_states: List[MeterState]


Expand All @@ -34,7 +34,7 @@ async def run_on_each(
max_at_once: int = None,
max_per_second: float = None,
_include_index: bool = False,
_send_to: MemorySendChannel = None,
_send_to: MemoryObjectSendStream = None,
) -> None:
meters: List[Meter] = []

Expand All @@ -57,4 +57,4 @@ async def run_on_each(
for state in meter_states:
await state.notify_task_started()

await task_group.spawn(_worker, async_fn, index, value, config)
task_group.start_soon(_worker, async_fn, index, value, config)
2 changes: 1 addition & 1 deletion tests/test_aiometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(self, num_tasks: int) -> None:
self.start_times: List[float] = []

async def task(self, *args: Any) -> None:
time = await anyio.current_time()
time = float(anyio.current_time())
self.start_times.append(time)

@property
Expand Down