Skip to content

Anyio3 #7

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A Redis client for Python supporting many Redis features and Python synchronous

- Transparent API (Just call the Redis commands, and the library will figure out cluster routing, script caching, etc...)
- Per context and command properties (database #, decoding, RESP3 attributes)
- Asynchronous I/O support with the same exact API (but with the await keyword), targeting asyncio, trio and curio (using [AnyIO](https://github.com/agronholm/anyio) which needs to be installed as well if you want async I/O)
- Asynchronous I/O support with the same exact API (but with the await keyword), targeting asyncio and trio (using [AnyIO](https://github.com/agronholm/anyio) which needs to be installed as well if you want async I/O)
- Modular API allowing for easy support for multiple synchronous and asynchronous event loops and disabling of unneeded features
- CI Testing for CPython 3.5, 3.6, 3.7, 3.8, 3.9 and PyPy3 with Redis 5 and Redis 6
- No legacy support for old language features
Expand Down
8 changes: 4 additions & 4 deletions justredis/nonsync/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ async def take(self):
if not conn.closed():
break
if self._limit is not None:
await self._limit.release()
self._limit.release()
except IndexError:
if self._limit is not None and not await self._limit.acquire(self._wait_timeout):
raise ConnectionPoolError("Could not acquire an connection form the pool")
try:
conn = await Connection.create(**self._connection_settings)
except Exception:
if self._limit is not None:
await self._limit.release()
self._limit.release()
raise
self._connections_in_use.add(conn)
return conn

async def release(self, conn):
async with self._shield():
with self._shield():
async with self._lock:
try:
self._connections_in_use.remove(conn)
Expand All @@ -77,7 +77,7 @@ async def release(self, conn):
if not conn.closed():
self._connections_available.append(conn)
elif self._limit is not None:
await self._limit.release()
self._limit.release()

async def __call__(self, *cmd, **kwargs):
if not cmd:
Expand Down
24 changes: 12 additions & 12 deletions justredis/nonsync/environments/anyio.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import anyio

try:
anyio.create_tcp_listener
anyio.Lock
except AttributeError:
raise AttributeError("You are using an old and incompatible AnyIO version, the minimum required version is AnyIO 2.0.0 .")
raise AttributeError("You are using an old and incompatible AnyIO version, the minimum required version is AnyIO 3.0.0 .")
import socket
import sys
import ssl
Expand All @@ -21,7 +21,7 @@
async def tcpsocket(address=None, connect_timeout=None, tcp_keepalive=None, tcp_nodelay=None, **kwargs):
if address is None:
address = ("localhost", 6379)
async with anyio.fail_after(connect_timeout):
with anyio.fail_after(connect_timeout):
sock = await anyio.connect_tcp(address[0], address[1])
if tcp_nodelay is not None:
if tcp_nodelay:
Expand All @@ -44,7 +44,7 @@ async def tcpsocket(address=None, connect_timeout=None, tcp_keepalive=None, tcp_
async def unixsocket(address=None, connect_timeout=None, **kwargs):
if address is None:
address = "/tmp/redis.sock"
async with anyio.fail_after(connect_timeout):
with anyio.fail_after(connect_timeout):
sock = await anyio.connect_unix(address)
return sock

Expand Down Expand Up @@ -85,7 +85,7 @@ async def aclose(self, force=False):

async def send(self, data):
if self._socket_timeout:
async with anyio.fail_after(self._socket_timeout):
with anyio.fail_after(self._socket_timeout):
await self._socket.send(data)
else:
await self._socket.send(data)
Expand All @@ -96,7 +96,7 @@ async def recv(self, timeout=False):
timeout = self._socket_timeout
if timeout:
try:
async with anyio.fail_after(timeout):
with anyio.fail_after(timeout):
return await self._socket.receive(self._buffersize)
except TimeoutError:
return None
Expand All @@ -112,14 +112,14 @@ def peername(self):

class OurSemaphore:
def __init__(self, value):
self._semaphore = anyio.create_capcity_limiter(value)
self._semaphore = anyio.CapacityLimiter(value)

async def release(self):
await self._semaphore.release()
def release(self):
self._semaphore.release()

async def acquire(self, timeout=None):
if timeout:
async with anyio.fail_after(timeout):
with anyio.fail_after(timeout):
await self._semaphore.acquire()
else:
await self._semaphore.acquire()
Expand All @@ -144,12 +144,12 @@ def semaphore(limit):

@staticmethod
def lock():
return anyio.create_lock()
return anyio.Lock()

# async only?
@staticmethod
def shield():
return anyio.open_cancel_scope(shield=True)
return anyio.CancelScope(shield=True)

# async only?
@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = justredis
version = 0.0.1a3
version = 0.0.1a4
description = A Redis client for Python supporting many Redis features and Python synchronous (Python 3.5+) and asynchronous (Python 3.6+) communication.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
6 changes: 3 additions & 3 deletions tests/async/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ async def test_cancel(client):
r = client

async with anyio.create_task_group() as tg:
await tg.spawn(r, "blpop", "a", 20)
tg.start_soon(r, "blpop", "a", 20)
await anyio.sleep(1)
await tg.cancel_scope.cancel()
tg.cancel_scope.cancel()

await anyio.sleep(1)
async with anyio.create_task_group() as tg:
await tg.spawn(r, "blpop", "a", 1)
tg.start_soon(r, "blpop", "a", 1)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ skip_missing_interpreters = true
deps =
pytest
pytest-cov
py{36,37,38,39,py3}: anyio[trio,curio]
py{36,37,38,39,py3}: anyio[trio]
commands =
py{35,36,37,38,39,py3}: pytest --cov={toxinidir}/justredis --cov={toxinidir}/tests --cov-append --cov-report=term-missing {posargs}
passenv =
Expand Down