Skip to content

Commit

Permalink
Use Lock(fast_acquire=True)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Sep 14, 2024
1 parent 891378f commit cd8b946
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ jobs:

- name: Check types
if: ${{ (matrix.python-version == '3.12') && (matrix.os == 'ubuntu-latest') }}
run: mypy src/anycorn/ tests/
run: |
python -m pip install git+https://github.com/agronholm/anyio.git@master
mypy src/anycorn/ tests/
- name: Run tests
run: |
pytest -k "not test_performances"
python -m pip install git+https://github.com/agronholm/anyio.git@master
pytest -k "not test_performances"
pytest -k "test_performances" -s
Expand Down
2 changes: 1 addition & 1 deletion src/anycorn/tcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
self.config = config
self.context = context
self.protocol: ProtocolWrapper
self.send_lock = Lock()
self.send_lock = Lock(fast_acquire=True)
self.idle_task = AnyioSingleTask()
self.state = state
self.stream = stream
Expand Down
11 changes: 6 additions & 5 deletions src/anycorn/worker_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
from typing import Awaitable, Callable

import anyio
from anyio import current_time, sleep
from anyio import TASK_STATUS_IGNORED, CancelScope, Lock, current_time, sleep
from anyio.abc import TaskStatus

from .typing import Event, SingleTask, TaskGroup


def _cancel_wrapper(func: Callable[[], Awaitable[None]]) -> Callable[[], Awaitable[None]]:
@wraps(func)
async def wrapper(
task_status: anyio.abc.TaskStatus[anyio.CancelScope] = anyio.TASK_STATUS_IGNORED,
task_status: TaskStatus[CancelScope] = TASK_STATUS_IGNORED,
) -> None:
cancel_scope = anyio.CancelScope()
cancel_scope = CancelScope()
task_status.started(cancel_scope)
with cancel_scope:
await func()
Expand All @@ -24,8 +25,8 @@ async def wrapper(

class AnyioSingleTask:
def __init__(self) -> None:
self._handle: anyio.CancelScope | None = None
self._lock = anyio.Lock()
self._handle: CancelScope | None = None
self._lock = Lock(fast_acquire=True)

async def restart(self, task_group: TaskGroup, action: Callable) -> None:
async with self._lock:
Expand Down

0 comments on commit cd8b946

Please sign in to comment.