用于python/asyncio的快速tarantool数据库连接器。
asynctnt的Python项目详细描述
<;图片 src=“https://avatars2.githubusercontent.com/u/2344919?v=2&s=250” align=“right”>;
asynctnt是一个高性能的Tarantool python/asyncio的数据库连接器库。它的灵感来自 asyncpg模块。
AsyncTnt需要Python3.5或更高版本,并支持Tarantool 版本1.6+。
安装
使用PIP安装:
$ pip install asynctnt
文档
文件可用 here。
主要功能
- 支持Tarantool支持的所有基本请求。这个 包括:insert,select,update,upsert,eval, sql(用于tarantool 2.x)、call和call16。注意:对于 “call16”与“call”的区别请参考塔兰托 文档。
- schema在建立连接时获取,因此可以使用 空格和索引名称,而不是它们的id。
- 架构自动重新蚀刻。如果塔兰托尔的模式改变了, asynctnt重新读取。
- 自动重新连接。如果由于某种原因失去连接-asynctnt 将启动自动重新连接过程(经授权和 当然是模式获取)。
- 能够对元组使用dict并将字段名用作dml中的键 请求(select、insert、replace、delete、update、upsert)。这是 只有在tarantool中指定space.format时才可能。字段名 也可用于更新操作,而不是字段号。 此外,元组被解码成特殊的结构,这些结构可以起作用 作为tuples或通过dicts使用适当的api。
- 所有请求都支持timeout值的规范,因此如果 请求执行时间过长,引发asyncio.timeouterror。它 大大简化了代码,因为您不需要使用 asyncio.wait_for(...)东西了。
基本用法
塔兰托配置:
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'},})end)
python代码:
importasyncioimportasynctntasyncdefmain():conn=asynctnt.Connection(host='127.0.0.1',port=3301)awaitconn.connect()foriinrange(1,11):awaitconn.insert('tester',[i,'hello{}'.format(i)])data=awaitconn.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"]}')awaitconn.disconnect()asyncio.run(main())
标准输出:
(注意,可以通过索引或 他们的名字)
tuple: <TarantoolTuple id=1 name='hello1'> tuple[0]: 1; tuple["id"]: 1 tuple[1]: hello1; tuple["name"]: hello1
SQL
Tarantool 2为数据库提供了一个SQL接口。你很容易 通过asynctnt
使用SQLbox.cfg{listen='127.0.0.1:3301'}box.once('v1',function()box.schema.user.grant('guest','read,write,execute','universe')box.sql.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.sql("insert into users (id, name) values (1, 'James Bond')")awaitconn.sql("insert into users (id, name) values (2, 'Ethan Hunt')")data=awaitconn.sql('select * from users')forrowindata:print(row)awaitconn.disconnect()asyncio.run(main())
标准输出:
<TarantoolTuple ID=1 NAME='James Bond'> <TarantoolTuple ID=2 NAME='Ethan Hunt'>
性能
在下面的所有基准上wal_mode = none
顺序
运行40K请求时的RPS(无^{TT16}$):
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 |
运行40K请求时的RPS(使用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 |
平行协程
在300个并行协程中运行200k个请求的rps(nouvloop):
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 |
让我们启用uvloop。这就是AsyncTnt的亮点。运行200K时的RPS 300个并行协同路由中的请求(使用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 |
许可证
AsyncTNT是在Apache2.0许可下开发和分发的。
参考文献
- Tarantool-内存数据库和 应用服务器。
- aiotarantool- 可选的python/asyncio连接器
- asynctnt-queue- 的asynctnt上的绑定 tarantool-queue