Package: asynctnt
asynctntasynctnt is a high-performanceTarantooldatabase
connector library for Python/asyncio. It was highly inspired byasyncpgmodule.asynctnt requires Python 3.7 or later and is supported for Tarantool
versions 1.10+.InstallationUse pip to install:$pipinstallasynctntDocumentationDocumentation is availablehere.Key featuresSupport for all thebasic requeststhat Tarantool supports. This includes:insert,select,update,upsert,call,eval,execute.Full support forSQL,
includingprepared statements.Support forinteractive transactionvia Tarantool streams.Support ofDecimal,UUIDanddatetimetypes natively.Support for parsingcustom errors.Schema fetchingon connection establishment, so you can use spaces and
indexes names rather than their ids, andauto refetchingif schema in
Tarantool is changedAuto reconnect. If connection is lost for some reason - asynctnt will
start automatic reconnection procedure (with authorization and schema
fetching, of course).Ability to usedicts for tupleswith 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 astuples or bydicts with
the appropriate API.All requests support specification oftimeoutvalue, so if request is
executed for too long, asyncio.TimeoutError is raised.Basic UsageTarantool config:box.cfg{listen='127.0.0.1:3301'}box.once('v1',function()box.schema.user.grant('guest','read,write,execute','universe')locals=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:importuuidimportasyncioimportasynctntasyncdefmain():conn=asynctnt.Connection(host='127.0.0.1',port=3301)awaitconn.connect()foriinrange(1,11):awaitconn.insert('tester',[i,'hello{}'.format(i),uuid.uuid4()])data=awaitconn.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"]=}')awaitconn.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')SQLTarantool 2.x brought out an SQL interface to the database. You can easily use it
inasynctntbox.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)importasyncioimportasynctntasyncdefmain():conn=asynctnt.Connection(host='127.0.0.1',port=3301)awaitconn.connect()awaitconn.execute("insert into users (id, name) values (?, ?)",[1,'James Bond'])awaitconn.execute("insert into users (id, name) values (?, ?)",[2,'Ethan Hunt'])data=awaitconn.execute('select * from users')forrowindata:print(row)awaitconn.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 thedocumentationPerformanceTwo performance tests were conducted:Seq-- Sequentially calling 40k requests and measuring performanceParallel-- Sending 200k in 300 parallel coroutinesOn all the benchmarks belowwal_mode = none.
Turninguvloopon has a massive effect on the performance, so it is recommended to useasynctntwith itBenchmark environmentMacBook Pro 2020CPU: 2 GHz Quad-Core Intel Core i5Memory: 16GB 3733 MHz LPDDR4XTarantool:box.cfg{wal_mode='none'}Seq (uvloop=off)Seq (uvloop=on)Parallel (uvloop=off)Parallel (uvloop=on)ping12940.9319980.8288341.95215756.24call11586.3818783.5674651.40137557.25eval10631.1917040.5761077.84121542.42select9613.8816718.9761584.07152526.21insert10077.1016989.0665594.82135491.25update10832.1616562.8063003.31121892.28execute10431.7516967.8558377.8196891.61Licenseasynctnt is developed and distributed under the Apache 2.0 license.ReferencesTarantool- in-memory database and application server.aiotarantool- alternative Python/asyncio connectorasynctnt-queue- bindings on top ofasynctntfortarantool-queue