Skip to main content

A fast Tarantool Database connector for Python/asyncio.

Project description

asynctnt

Build PyPI Maintainability

asynctnt is a high-performance Tarantool database connector library for Python/asyncio. It was highly inspired by asyncpg module.

asynctnt requires Python 3.7 or later and is supported for Tarantool versions 1.10+.

Installation

Use pip to install:

$ pip install asynctnt

Documentation

Documentation is available here.

Key features

  • Support for all the basic requests that Tarantool supports. This includes: insert, select, update, upsert, call, eval, execute.
  • Full support for SQL, including prepared statements.
  • Support for interactive transaction via Tarantool streams.
  • Support of Decimal, UUID,datetime types natively.
  • Support for interval types.
  • Support for parsing custom errors.
  • Schema fetching on connection establishment, so you can use spaces and indexes names rather than their ids, and auto refetching if schema in Tarantool is changed
  • Auto reconnect. If connection is lost for some reason - asynctnt will start automatic reconnection procedure (with authorization and schema fetching, of course).
  • Ability to use dicts for tuples with field names as keys in DML requests (select, insert, replace, delete, update, upsert). This is possible only if space.format is specified in Tarantool. Field names can also be used in update operations instead of field numbers. Moreover, tuples are decoded into the special structures that can act either as tuples or by dicts with the appropriate API.
  • All requests support specification of timeout value, so if request is executed for too long, asyncio.TimeoutError is raised.

Basic Usage

Tarantool config:

box.cfg {
    listen = '127.0.0.1:3301'
}

box.once('v1', function()
    box.schema.user.grant('guest', 'read,write,execute', 'universe')

    local s = box.schema.create_space('tester')
    s:create_index('primary')
    s:format({
        { name = 'id', type = 'unsigned' },
        { name = 'name', type = 'string' },
        { name = 'uuid', type = 'uuid' },
    })
end)

Python code:

import uuid
import asyncio
import asynctnt


async def main():
    conn = asynctnt.Connection(host='127.0.0.1', port=3301)
    await conn.connect()

    for i in range(1, 11):
        await conn.insert('tester', [i, 'hello{}'.format(i), uuid.uuid4()])

    data = await conn.select('tester', [])
    tup = data[0]
    print('tuple:', tup)
    print(f'{tup[0]=}; {tup["id"]=}')
    print(f'{tup[1]=}; {tup["name"]=}')
    print(f'{tup[2]=}; {tup["uuid"]=}')

    await conn.disconnect()


asyncio.run(main())

Stdout:

(note that you can simultaneously access fields either by indices or by their names)

tuple: <TarantoolTuple id=1 name='hello1' uuid=UUID('ebbad14c-f78c-42e8-bd12-bfcc564443a6')>
tup[0]=1; tup["id"]=1
tup[1]='hello1'; tup["name"]='hello1'
tup[2]=UUID('ebbad14c-f78c-42e8-bd12-bfcc564443a6'); tup["uuid"]=UUID('ebbad14c-f78c-42e8-bd12-bfcc564443a6')

SQL

Tarantool 2.x brought out an SQL interface to the database. You can easily use it in asynctnt

box.cfg {
    listen = '127.0.0.1:3301'
}

box.once('v1', function()
    box.schema.user.grant('guest', 'read,write,execute', 'universe')

    box.execute([[
        create table users (
            id int primary key,
            name text
        )
    ]])
end)
import asyncio
import asynctnt


async def main():
    conn = asynctnt.Connection(host='127.0.0.1', port=3301)
    await conn.connect()

    await conn.execute("insert into users (id, name) values (?, ?)", [1, 'James Bond'])
    await conn.execute("insert into users (id, name) values (?, ?)", [2, 'Ethan Hunt'])
    data = await conn.execute('select * from users')

    for row in data:
        print(row)

    await conn.disconnect()

asyncio.run(main())

Stdout:

<TarantoolTuple ID=1 NAME='James Bond'>
<TarantoolTuple ID=2 NAME='Ethan Hunt'>

More about SQL features in asynctnt please refer to the documentation

Performance

Two performance tests were conducted:

  1. Seq -- Sequentially calling 40k requests and measuring performance
  2. Parallel -- Sending 200k in 300 parallel coroutines

On all the benchmarks below wal_mode = none. Turning uvloop on has a massive effect on the performance, so it is recommended to use asynctnt with it

Benchmark environment

  • MacBook Pro 2020
  • CPU: 2 GHz Quad-Core Intel Core i5
  • Memory: 16GB 3733 MHz LPDDR4X

Tarantool:

box.cfg{wal_mode = 'none'}
Seq (uvloop=off) Seq (uvloop=on) Parallel (uvloop=off) Parallel (uvloop=on)
ping 12940.93 19980.82 88341.95 215756.24
call 11586.38 18783.56 74651.40 137557.25
eval 10631.19 17040.57 61077.84 121542.42
select 9613.88 16718.97 61584.07 152526.21
insert 10077.10 16989.06 65594.82 135491.25
update 10832.16 16562.80 63003.31 121892.28
execute 10431.75 16967.85 58377.81 96891.61

License

asynctnt is developed and distributed under the Apache 2.0 license.

References

  1. Tarantool - in-memory database and application server.
  2. aiotarantool - alternative Python/asyncio connector
  3. asynctnt-queue - bindings on top of asynctnt for tarantool-queue

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asynctnt-2.3.0.tar.gz (552.1 kB view hashes)

Uploaded Source

Built Distributions

asynctnt-2.3.0-pp310-pypy310_pp73-win_amd64.whl (311.0 kB view hashes)

Uploaded PyPy Windows x86-64

asynctnt-2.3.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (389.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

asynctnt-2.3.0-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (400.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

asynctnt-2.3.0-pp310-pypy310_pp73-macosx_11_0_arm64.whl (310.3 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

asynctnt-2.3.0-cp312-cp312-win_amd64.whl (324.5 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

asynctnt-2.3.0-cp312-cp312-win32.whl (283.8 kB view hashes)

Uploaded CPython 3.12 Windows x86

asynctnt-2.3.0-cp312-cp312-musllinux_1_1_x86_64.whl (2.2 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

asynctnt-2.3.0-cp312-cp312-musllinux_1_1_i686.whl (2.1 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

asynctnt-2.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

asynctnt-2.3.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

asynctnt-2.3.0-cp312-cp312-macosx_11_0_arm64.whl (359.3 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

asynctnt-2.3.0-cp311-cp311-win_amd64.whl (327.3 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

asynctnt-2.3.0-cp311-cp311-win32.whl (286.0 kB view hashes)

Uploaded CPython 3.11 Windows x86

asynctnt-2.3.0-cp311-cp311-musllinux_1_1_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

asynctnt-2.3.0-cp311-cp311-musllinux_1_1_i686.whl (2.2 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

asynctnt-2.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

asynctnt-2.3.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

asynctnt-2.3.0-cp311-cp311-macosx_11_0_arm64.whl (352.2 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

asynctnt-2.3.0-cp310-cp310-win_amd64.whl (323.6 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

asynctnt-2.3.0-cp310-cp310-win32.whl (285.1 kB view hashes)

Uploaded CPython 3.10 Windows x86

asynctnt-2.3.0-cp310-cp310-musllinux_1_1_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

asynctnt-2.3.0-cp310-cp310-musllinux_1_1_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

asynctnt-2.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

asynctnt-2.3.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

asynctnt-2.3.0-cp310-cp310-macosx_11_0_arm64.whl (350.3 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

asynctnt-2.3.0-cp39-cp39-win_amd64.whl (323.9 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

asynctnt-2.3.0-cp39-cp39-win32.whl (285.3 kB view hashes)

Uploaded CPython 3.9 Windows x86

asynctnt-2.3.0-cp39-cp39-musllinux_1_1_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

asynctnt-2.3.0-cp39-cp39-musllinux_1_1_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

asynctnt-2.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

asynctnt-2.3.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

asynctnt-2.3.0-cp39-cp39-macosx_11_0_arm64.whl (350.4 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

asynctnt-2.3.0-cp38-cp38-win_amd64.whl (325.5 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

asynctnt-2.3.0-cp38-cp38-win32.whl (286.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

asynctnt-2.3.0-cp38-cp38-musllinux_1_1_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

asynctnt-2.3.0-cp38-cp38-musllinux_1_1_i686.whl (2.2 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

asynctnt-2.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

asynctnt-2.3.0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

asynctnt-2.3.0-cp38-cp38-macosx_11_0_arm64.whl (350.5 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

asynctnt-2.3.0-cp37-cp37m-win_amd64.whl (315.2 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

asynctnt-2.3.0-cp37-cp37m-win32.whl (278.0 kB view hashes)

Uploaded CPython 3.7m Windows x86

asynctnt-2.3.0-cp37-cp37m-musllinux_1_1_x86_64.whl (1.9 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

asynctnt-2.3.0-cp37-cp37m-musllinux_1_1_i686.whl (1.8 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

asynctnt-2.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.9 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

asynctnt-2.3.0-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.8 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page