Asyncio Examples#

All commands are coroutine functions.

Connecting and Disconnecting#

Using asyncio Valkey requires an explicit disconnect of the connection since there is no asyncio deconstructor magic method. By default, an internal connection pool is created on valkey.Valkey() and attached to the Valkey instance. When calling Valkey.aclose this internal connection pool closes automatically, which disconnects all connections.

[1]:
import valkey.asyncio as valkey

client = valkey.Valkey()
print(f"Ping successful: {await client.ping()}")
await client.aclose()
Ping successful: True

If you create a custom ConnectionPool to be used by a single Valkey instance, use the Valkey.from_pool class method. The Valkey client will take ownership of the connection pool. This will cause the pool to be disconnected along with the Valkey instance. Disconnecting the connection pool simply disconnects all connections hosted in the pool.

[ ]:
import valkey.asyncio as valkey

pool = valkey.ConnectionPool.from_url("valkey://localhost")
client = valkey.Valkey.from_pool(pool)
await client.aclose()

However, if the ConnectionPool is to be shared by several Valkey instances, you should use the connection_pool argument, and you may want to disconnect the connection pool explicitly.

[2]:
import valkey.asyncio as valkey

pool = valkey.ConnectionPool.from_url("valkey://localhost")
client1 = valkey.Valkey(connection_pool=pool)
client2 = valkey.Valkey(connection_pool=pool)
await client1.aclose()
await client2.aclose()
await pool.aclose()

By default, this library uses version 2 of the RESP protocol. To enable RESP version 3, you will want to set protocol to 3

[ ]:
import valkey.asyncio as valkey

client = valkey.Valkey(protocol=3)
await client.aclose()
await client.ping()

Transactions (Multi/Exec)#

The aiovalkey.Valkey.pipeline will return a aiovalkey.Pipeline object, which will buffer all commands in-memory and compile them into batches using the Valkey Bulk String protocol. Additionally, each command will return the Pipeline instance, allowing you to chain your commands, i.e., p.set(‘foo’, 1).set(‘bar’, 2).mget(‘foo’, ‘bar’).

The commands will not be reflected in Valkey until execute() is called & awaited.

Usually, when performing a bulk operation, taking advantage of a “transaction” (e.g., Multi/Exec) is to be desired, as it will also add a layer of atomicity to your bulk operation.

[3]:
import valkey.asyncio as valkey

r = await valkey.from_url("valkey://localhost")
async with r.pipeline(transaction=True) as pipe:
    ok1, ok2 = await (pipe.set("key1", "value1").set("key2", "value2").execute())
assert ok1
assert ok2

Pub/Sub Mode#

Subscribing to specific channels:

[4]:
import asyncio

import valkey.asyncio as valkey

STOPWORD = "STOP"


async def reader(channel: valkey.client.PubSub):
    while True:
        message = await channel.get_message(ignore_subscribe_messages=True, timeout=None)
        if message is not None:
            print(f"(Reader) Message Received: {message}")
            if message["data"].decode() == STOPWORD:
                print("(Reader) STOP")
                break

r = valkey.from_url("valkey://localhost")
async with r.pubsub() as pubsub:
    await pubsub.subscribe("channel:1", "channel:2")

    future = asyncio.create_task(reader(pubsub))

    await r.publish("channel:1", "Hello")
    await r.publish("channel:2", "World")
    await r.publish("channel:1", STOPWORD)

    await future
(Reader) Message Received: {'type': 'message', 'pattern': None, 'channel': b'channel:1', 'data': b'Hello'}
(Reader) Message Received: {'type': 'message', 'pattern': None, 'channel': b'channel:2', 'data': b'World'}
(Reader) Message Received: {'type': 'message', 'pattern': None, 'channel': b'channel:1', 'data': b'STOP'}
(Reader) STOP

Subscribing to channels matching a glob-style pattern:

[5]:
import asyncio

import valkey.asyncio as valkey

STOPWORD = "STOP"


async def reader(channel: valkey.client.PubSub):
    while True:
        message = await channel.get_message(ignore_subscribe_messages=True, timeout=None)
        if message is not None:
            print(f"(Reader) Message Received: {message}")
            if message["data"].decode() == STOPWORD:
                print("(Reader) STOP")
                break


r = await valkey.from_url("valkey://localhost")
async with r.pubsub() as pubsub:
    await pubsub.psubscribe("channel:*")

    future = asyncio.create_task(reader(pubsub))

    await r.publish("channel:1", "Hello")
    await r.publish("channel:2", "World")
    await r.publish("channel:1", STOPWORD)

    await future
(Reader) Message Received: {'type': 'pmessage', 'pattern': b'channel:*', 'channel': b'channel:1', 'data': b'Hello'}
(Reader) Message Received: {'type': 'pmessage', 'pattern': b'channel:*', 'channel': b'channel:2', 'data': b'World'}
(Reader) Message Received: {'type': 'pmessage', 'pattern': b'channel:*', 'channel': b'channel:1', 'data': b'STOP'}
(Reader) STOP

Sentinel Client#

The Sentinel client requires a list of Valkey Sentinel addresses to connect to and start discovering services.

Calling aiovalkey.sentinel.Sentinel.master_for or aiovalkey.sentinel.Sentinel.slave_for methods will return Valkey clients connected to specified services monitored by Sentinel.

Sentinel client will detect failover and reconnect Valkey clients automatically.

[ ]:
import asyncio

from valkey.asyncio.sentinel import Sentinel


sentinel = Sentinel([("localhost", 26379), ("sentinel2", 26379)])
r = sentinel.master_for("mymaster")

ok = await r.set("key", "value")
assert ok
val = await r.get("key")
assert val == b"value"

Connecting to Valkey instances by specifying a URL scheme.#

Parameters are passed to the following schems, as parameters to the url scheme.

Three URL schemes are supported:

  • valkey:// creates a TCP socket connection.

  • valkeys:// creates a SSL wrapped TCP socket connection.

  • unix://: creates a Unix Domain Socket connection.

[ ]:
import valkey.asyncio as valkey
url_connection = valkey.from_url("valkey://localhost:6379?decode_responses=True")
url_connection.ping()
True

To enable the RESP 3 protocol, append protocol=3 to the URL.

[ ]:
import valkey.asyncio as valkey

url_connection = valkey.from_url("valkey://localhost:6379?decode_responses=True&protocol=3")
url_connection.ping()