zlmdb._lmdb_vendor.aio

Async wrappers for py-lmdb via asyncio.loop.run_in_executor().

Usage:

import lmdb
import lmdb.aio

env = lmdb.open('/tmp/mydb')
aenv = lmdb.aio.wrap(env)

async with aenv.begin(write=True) as txn:
    await txn.put(b'key', b'value')
    val = await txn.get(b'key')

Classes

AsyncCursor

Async wrapper for lmdb.Cursor.

AsyncEnvironment

Async wrapper for lmdb.Environment.

AsyncTransaction

Async wrapper for lmdb.Transaction.

_AsyncContextWrapper

Wraps a coroutine so it can be used as both await and async with.

Functions

_async_method(sync)

Return a coroutine method that calls sync in the executor.

_async_method_locked(sync)

Like _async_method(), but acquires self._lock first.

_collect_locked(sync)

Like _async_method_locked(), but sync returns an iterator

_sync_method(sync)

Return a method that calls sync directly, without an executor.

wrap(env[, executor])

Wrap an lmdb.Environment for async use.

Module Contents

class AsyncCursor(cursor, executor=None, lock=None)[source]

Async wrapper for lmdb.Cursor.

All methods of the underlying Cursor are available. Most are dispatched to an executor; key(), value(), and item() are called directly.

Iterator methods (iternext(), iterprev(), etc.) are consumed in the executor and returned as a list.

Shares the parent transaction’s asyncio.Lock.

Supports async with — the cursor is closed on exit.

_WRAPS = '_cursor'[source]
async __aenter__()[source]
async __aexit__(*_exc)[source]
__getattr__(name)[source]
__slots__ = ('_cursor', '_executor', '_lock')[source]
_cursor[source]
_executor = None[source]
_lock[source]
close[source]
count[source]
delete[source]
first[source]
first_dup[source]
get[source]
getmulti[source]
item[source]
iternext[source]
iternext_dup[source]
iternext_nodup[source]
iterprev[source]
iterprev_dup[source]
iterprev_nodup[source]
key[source]
last[source]
last_dup[source]
next[source]
next_dup[source]
next_nodup[source]
pop[source]
prev[source]
prev_dup[source]
prev_nodup[source]
put[source]
putmulti[source]
replace[source]
set_key[source]
set_key_dup[source]
set_range[source]
set_range_dup[source]
value[source]
class AsyncEnvironment(env, executor=None)[source]

Async wrapper for lmdb.Environment.

Created by wrap(). All methods of the underlying Environment are available and are dispatched to an executor, except for the low-overhead accessors path(), max_key_size(), max_readers(), and flags(), which are called directly.

Supports async with for lifetime management — the environment is closed on exit.

_WRAPS = '_env'[source]
async __aenter__()[source]
async __aexit__(*_exc)[source]
__getattr__(name)[source]
__slots__ = ('_env', '_executor')[source]
_env[source]
_executor = None[source]
begin(*args, **kwargs)[source]

Start a new transaction, returning an AsyncTransaction.

Accepts the same arguments as lmdb.Environment.begin(). Can be used with await or async with:

async with aenv.begin(write=True) as txn:
    await txn.put(b'key', b'value')
close[source]
copy[source]
copyfd[source]
dbs[source]
flags[source]
info[source]
max_key_size[source]
max_readers[source]
open_db[source]
path[source]
reader_check[source]
readers[source]
set_mapsize[source]
stat[source]
sync[source]
class AsyncTransaction(txn, executor=None)[source]

Async wrapper for lmdb.Transaction.

All methods of the underlying Transaction are available. Most are dispatched to an executor; id() is called directly.

An asyncio.Lock serializes all operations dispatched through this transaction, including operations on its cursors. This makes asyncio.gather() safe on the same transaction.

Supports async with — write transactions are committed on clean exit and aborted on exception.

_WRAPS = '_txn'[source]
async __aenter__()[source]
async __aexit__(exc_type, _exc_val, _exc_tb)[source]
__getattr__(name)[source]
__slots__ = ('_txn', '_executor', '_lock')[source]
_executor = None[source]
_lock[source]
_txn[source]
abort[source]
commit[source]
cursor(*args, **kwargs)[source]

Open a cursor, returning an AsyncCursor.

Accepts the same arguments as lmdb.Transaction.cursor(). Can be used with await or async with:

async with txn.cursor() as cur:
    await cur.first()
    items = await cur.iternext()
delete[source]
drop[source]
get[source]
id[source]
pop[source]
put[source]
replace[source]
stat[source]
class _AsyncContextWrapper(coro)[source]

Wraps a coroutine so it can be used as both await and async with.

Supports:

txn = await aenv.begin(write=True)      # just await
async with aenv.begin(write=True) as txn:  # context manager
async __aenter__()[source]
async __aexit__(exc_type, exc_val, exc_tb)[source]
__await__()[source]
__slots__ = ('_coro', '_result')[source]
_coro[source]
_result = None[source]
_async_method(sync)[source]

Return a coroutine method that calls sync in the executor.

_async_method_locked(sync)[source]

Like _async_method(), but acquires self._lock first.

_collect_locked(sync)[source]

Like _async_method_locked(), but sync returns an iterator consumed in the executor and returned as a list.

_sync_method(sync)[source]

Return a method that calls sync directly, without an executor.

wrap(env, executor=None)[source]

Wrap an lmdb.Environment for async use.

executor is passed to loop.run_in_executor(). None (the default) uses the loop’s default executor.