Skip to main content

A fast Tarantool Database connector for Python/asyncio.

Project description

Build Status PyPI Maintainability <img src=”https://avatars2.githubusercontent.com/u/2344919?v=2&s=250” align=”right”>

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

asynctnt requires Python 3.5 or later and is supported for Tarantool versions 1.6+.

Installation

Use pip to install:

$ pip install asynctnt

Documentation

Documentation is available here.

Key features

  • Support for all of the basic requests that Tarantool supports. This includes: insert, select, update, upsert, eval, sql (for Tarantool 2.x), call and call16. Note: For the difference between ``call16`` and ``call`` please refer to Tarantool documentation.

  • Schema fetching on connection establishment, so you can use spaces and indexes names rather than their ids.

  • Schema auto refetching. If schema in Tarantool is changed, asynctnt refetches it.

  • 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. It drastically simplifies your code, as you don’t need to use asyncio.wait_for(...) stuff anymore.

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' },
    })
end)

Python code:

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)])

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

    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'>
tuple[0]: 1; tuple["id"]: 1
tuple[1]: hello1; tuple["name"]: hello1

SQL

Tarantool 2 brings out an SQL interface to the database. You can easily use SQL through 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.sql("insert into users (id, name) values (1, 'James Bond')")
    await conn.sql("insert into users (id, name) values (2, 'Ethan Hunt')")
    data = await conn.sql('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'>

Performance

On all of the benchmarks below wal_mode = none

Sequential

RPS on running 40k requests (no uvloop):

Request

aiotarantool

asynctnt

ping

5010.60

9037.07

call

4575.98

9113.32

eval

4096.32

8921.95

select

4063.15

9681.12

insert

4038.04

9332.21

update

3945.12

10532.75

RPS on running 40k requests (with uvloop):

Request

aiotarantool

asynctnt

ping

7204.31

20372.59

call

6723.58

17279.21

eval

7001.27

16642.67

select

7028.03

17730.24

insert

7054.06

17384.26

update

6618.01

15990.12

Parallel coroutines

RPS on running 200k requests in 300 parallel coroutines (no uvloop):

Request

aiotarantool

asynctnt

ping

32946.25

44090.53

call

29005.93

41129.16

eval

28792.84

44097.02

select

26929.76

35853.33

insert

27142.52

31329.85

update

25330.98

36281.59

Let’s enable uvloop. This is where asynctnt shines. RPS on running 200k requests in 300 parallel coroutines (with uvloop):

Request

aiotarantool

asynctnt

ping

38962.01

134043.41

call

32799.71

99866.28

eval

27608.09

91056.69

select

27436.98

108940.41

insert

33247.57

102971.13

update

28544.68

98643.46

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-1.2.1.tar.gz (340.2 kB view hashes)

Uploaded Source

Built Distributions

asynctnt-1.2.1-cp39-cp39-win_amd64.whl (429.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

asynctnt-1.2.1-cp39-cp39-win32.whl (405.3 kB view hashes)

Uploaded CPython 3.9 Windows x86

asynctnt-1.2.1-cp39-cp39-manylinux2010_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

asynctnt-1.2.1-cp39-cp39-manylinux2010_i686.whl (1.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

asynctnt-1.2.1-cp39-cp39-manylinux1_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.9

asynctnt-1.2.1-cp39-cp39-manylinux1_i686.whl (1.3 MB view hashes)

Uploaded CPython 3.9

asynctnt-1.2.1-cp39-cp39-macosx_10_9_x86_64.whl (480.3 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

asynctnt-1.2.1-cp38-cp38-win_amd64.whl (429.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

asynctnt-1.2.1-cp38-cp38-win32.whl (405.8 kB view hashes)

Uploaded CPython 3.8 Windows x86

asynctnt-1.2.1-cp38-cp38-manylinux2010_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

asynctnt-1.2.1-cp38-cp38-manylinux2010_i686.whl (1.4 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

asynctnt-1.2.1-cp38-cp38-manylinux1_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.8

asynctnt-1.2.1-cp38-cp38-manylinux1_i686.whl (1.4 MB view hashes)

Uploaded CPython 3.8

asynctnt-1.2.1-cp38-cp38-macosx_10_9_x86_64.whl (477.5 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

asynctnt-1.2.1-cp37-cp37m-win_amd64.whl (421.9 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

asynctnt-1.2.1-cp37-cp37m-win32.whl (401.7 kB view hashes)

Uploaded CPython 3.7m Windows x86

asynctnt-1.2.1-cp37-cp37m-manylinux2010_x86_64.whl (1.2 MB view hashes)

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

asynctnt-1.2.1-cp37-cp37m-manylinux2010_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

asynctnt-1.2.1-cp37-cp37m-manylinux1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.7m

asynctnt-1.2.1-cp37-cp37m-manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.7m

asynctnt-1.2.1-cp37-cp37m-macosx_10_9_x86_64.whl (470.7 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

asynctnt-1.2.1-cp36-cp36m-win_amd64.whl (421.8 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

asynctnt-1.2.1-cp36-cp36m-win32.whl (401.8 kB view hashes)

Uploaded CPython 3.6m Windows x86

asynctnt-1.2.1-cp36-cp36m-manylinux2010_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

asynctnt-1.2.1-cp36-cp36m-manylinux2010_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

asynctnt-1.2.1-cp36-cp36m-manylinux1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.6m

asynctnt-1.2.1-cp36-cp36m-manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.6m

asynctnt-1.2.1-cp36-cp36m-macosx_10_9_x86_64.whl (480.9 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

asynctnt-1.2.1-cp35-cp35m-manylinux2010_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.5m manylinux: glibc 2.12+ x86-64

asynctnt-1.2.1-cp35-cp35m-manylinux2010_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.5m manylinux: glibc 2.12+ i686

asynctnt-1.2.1-cp35-cp35m-manylinux1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.5m

asynctnt-1.2.1-cp35-cp35m-manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.5m

asynctnt-1.2.1-cp35-cp35m-macosx_10_9_x86_64.whl (467.5 kB view hashes)

Uploaded CPython 3.5m macOS 10.9+ x86-64

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