Skip to content

Commit

Permalink
improve synchronization primitives (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut authored Jan 31, 2025
1 parent f57ee89 commit a8c8bba
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.9"
python-version: "3.13"
- name: Install Pre-Commit
run: python -m pip install pre-commit && pre-commit install
- name: Load cached Pre-Commit Dependencies
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: "3.9"
python: "3.13"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
Expand Down
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Unasyncd features:
* [`asyncio.TaskGroup`](#asynciotaskgroup)
* [`anyio.create_task_group`](#anyiocreatetaskgroup)
* [`asyncio.sleep` / `anyio.sleep`](#asynciosleep--anyiosleep)
* [`asyncio.Lock` / `anyio.Lock`](#asynciolock--anyiolock)
* [`asyncio.Event` / `anyio.Event`](#asyncioevent--anyioevent)
* [`asyncio.Barrier`](#asynciobarrier)
* [`anyio.Path`](#anyiopath)
* [Type annotations](#type-annotations)
* [Docstrings](#docstrings)
Expand Down Expand Up @@ -352,6 +355,89 @@ import asyncio
await asyncio.sleep(0)
```

### `asyncio.Lock` / `anyio.Lock`

*Async*
```python
import asyncio
# import anyio

lock = asyncio.Lock()

async with lock:
pass
```

*Sync*
```python
import threading

lock = threading.Lock()

with lock:
pass
```

### `asyncio.Event` / `anyio.Event`

*Async*
```python
import asyncio
# import anyio

event = asyncio.Event()

event.set()
await event.wait()
assert event.is_set()
event.clear()
```

*Sync*
```python
import threading

event = threading.Event()

event.set()
event.wait()
assert event.is_set()
event.clear()
```

### `asyncio.Barrier`

*Async*
```python
import asyncio

barrier = asyncio.Barrier(2)

await barrier.wait()
await barrier.reset()
await barrier.abort()

barrier.parties
barrier.n_waiting
barrier.broken
```

*Sync*
```python
import threading

barrier = threading.Barrier(2)

barrier.wait()
barrier.reset()
barrier.abort()

barrier.parties
barrier.n_waiting
barrier.broken
```


### `anyio.Path`

*Async*
Expand Down
189 changes: 189 additions & 0 deletions test/test_tree_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1875,3 +1875,192 @@ def foo():
"""

assert transformer(dedent(source)) == dedent(expected)


def test_asyncio_semaphore(transformer):
source = """
import asyncio
sem = asyncio.Semaphore(2)
await sem.acquire()
await sem.release()
async with sem:
pass
"""

expected = """
import asyncio
import threading
sem = threading.Semaphore(2)
sem.acquire()
sem.release()
with sem:
pass
"""

assert transformer(dedent(source)) == dedent(expected)


def test_anyio_semaphore(transformer):
source = """
import anyio
sem = anyio.Semaphore(2)
await sem.acquire()
await sem.release()
async with sem:
pass
"""

expected = """
import anyio
import threading
sem = threading.Semaphore(2)
sem.acquire()
sem.release()
with sem:
pass
"""

assert transformer(dedent(source)) == dedent(expected)


def test_asyncio_lock(transformer):
source = """
import asyncio
lock = asyncio.Lock()
async with lock:
pass
"""

expected = """
import asyncio
import threading
lock = threading.Lock()
with lock:
pass
"""

assert transformer(dedent(source)) == dedent(expected)


def test_anyio_lock(transformer):
source = """
import anyio
lock = anyio.Lock()
async with lock:
pass
"""

expected = """
import anyio
import threading
lock = threading.Lock()
with lock:
pass
"""

assert transformer(dedent(source)) == dedent(expected)


def test_asyncio_event(transformer):
source = """
import asyncio
event = asyncio.Event()
event.set()
await event.wait()
assert event.is_set()
event.clear()
"""

expected = """
import asyncio
import threading
event = threading.Event()
event.set()
event.wait()
assert event.is_set()
event.clear()
"""

assert transformer(dedent(source)) == dedent(expected)


def test_anyio_event(transformer):
source = """
import anyio
event = anyio.Event()
event.set()
await event.wait()
assert event.is_set()
"""

expected = """
import anyio
import threading
event = threading.Event()
event.set()
event.wait()
assert event.is_set()
"""

assert transformer(dedent(source)) == dedent(expected)


def test_asyncio_barrier(transformer):
source = """
import asyncio
barrier = asyncio.Barrier(2)
await barrier.wait()
await barrier.reset()
await barrier.abort()
barrier.parties
barrier.n_waiting
barrier.broken
"""

expected = """
import asyncio
import threading
barrier = threading.Barrier(2)
barrier.wait()
barrier.reset()
barrier.abort()
barrier.parties
barrier.n_waiting
barrier.broken
"""

assert transformer(dedent(source)) == dedent(expected)
7 changes: 7 additions & 0 deletions unasyncd/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
"asyncio.sleep": "time.sleep",
"anyio.sleep": "time.sleep",
"anyio.Path": "pathlib.Path",
"asyncio.Semaphore": "threading.Semaphore",
"anyio.Semaphore": "threading.Semaphore",
"asyncio.Lock": "threading.Lock",
"anyio.Lock": "threading.Lock",
"asyncio.Event": "threading.Event",
"anyio.Event": "threading.Event",
"asyncio.Barrier": "threading.Barrier",
}


Expand Down

0 comments on commit a8c8bba

Please sign in to comment.