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¶
Async wrapper for |
|
Async wrapper for |
|
Async wrapper for |
|
Wraps a coroutine so it can be used as both |
Functions¶
|
Return a coroutine method that calls sync in the executor. |
|
Like |
|
Like |
|
Return a method that calls sync directly, without an executor. |
|
Wrap an |
Module Contents¶
- class AsyncCursor(cursor, executor=None, lock=None)[source]¶
Async wrapper for
lmdb.Cursor.All methods of the underlying
Cursorare available. Most are dispatched to an executor;key(),value(), anditem()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.
- class AsyncEnvironment(env, executor=None)[source]¶
Async wrapper for
lmdb.Environment.Created by
wrap(). All methods of the underlyingEnvironmentare available and are dispatched to an executor, except for the low-overhead accessorspath(),lib_version(),max_key_size(),max_readers(), andflags(), which are called directly.Supports
async withfor lifetime management — the environment is closed on exit.- begin(*args, **kwargs)[source]¶
Start a new transaction, returning an
AsyncTransaction.Accepts the same arguments as
lmdb.Environment.begin(), except that nested transactions (parent=) are not supported. Can be used withawaitorasync with:async with aenv.begin(write=True) as txn: await txn.put(b'key', b'value')
A write transaction is given its own private single-thread executor, so
begin, every operation, andcommit/abortall run on one OS thread. LMDB ties a write transaction (and, on Linux, its robust process-shared write mutex) to the thread that began it, so dispatching its operations across a shared multi-threaded executor could release the mutex on the wrong thread (see issue #465). Read transactions are safe to migrate between threads underMDB_NOTLSand use the environment’s executor.
- class AsyncTransaction(txn, executor=None, owns_executor=False)[source]¶
Async wrapper for
lmdb.Transaction.All methods of the underlying
Transactionare available. Most are dispatched to an executor;id()is called directly.An
asyncio.Lockserializes all operations dispatched through this transaction, including operations on its cursors. This makesasyncio.gather()safe on the same transaction.Supports
async with— the transaction is committed on clean exit and aborted on exception.- cursor(*args, **kwargs)[source]¶
Open a cursor, returning an
AsyncCursor.Accepts the same arguments as
lmdb.Transaction.cursor(). Can be used withawaitorasync with:async with txn.cursor() as cur: await cur.first() items = await cur.iternext()
- class _AsyncContextWrapper(coro)[source]¶
Wraps a coroutine so it can be used as both
awaitandasync with.Supports:
txn = await aenv.begin(write=True) # just await async with aenv.begin(write=True) as txn: # context manager
- _async_method_locked(sync)[source]¶
Like
_async_method(), but acquiresself._lockfirst.
- _collect_locked(sync)[source]¶
Like
_async_method_locked(), but sync returns an iterator consumed in the executor and returned as a list.
- wrap(env, executor=None)[source]¶
Wrap an
lmdb.Environmentfor async use.executor is passed to
asyncio.loop.run_in_executor().None(the default) uses the loop’s default executor. Write transactions always run on a private single-thread executor instead; seeAsyncEnvironment.begin().