### Install asynctnt with pip Source: https://github.com/igorcoding/asynctnt/blob/main/docs/installation.md Instructions to install the asynctnt library through the pip package manager. This command will download and install the necessary packages from PyPI. ```bash $ pip install asynctnt ``` -------------------------------- ### Install asynctnt Python Library Source: https://github.com/igorcoding/asynctnt/blob/main/README.md This snippet shows how to install the asynctnt library using pip, the Python package installer. ```Bash $ pip install asynctnt ``` -------------------------------- ### Install asynctnt via pip Source: https://github.com/igorcoding/asynctnt/blob/main/docs/overview.md This snippet shows how to install the asynctnt library using the pip package manager, which is the standard way to install Python packages. ```bash $ pip install asynctnt ``` -------------------------------- ### Configure Tarantool for asynctnt basic usage Source: https://github.com/igorcoding/asynctnt/blob/main/docs/overview.md This Lua snippet configures a Tarantool instance to listen on a specific address and port, grants read/write/execute permissions to the 'guest' user, and creates a 'tester' space with a primary index and a defined format including 'id' (unsigned), 'name' (string), and 'uuid' (uuid) fields. This setup is required for the Python basic usage example to interact with the database. ```lua 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) ``` -------------------------------- ### Tarantool Lua: SQL Database and User Setup Source: https://github.com/igorcoding/asynctnt/blob/main/README.md This Lua code configures a Tarantool instance, setting up a listener, granting permissions to the 'guest' user, and creating a 'users' table. This is a foundational step for enabling SQL operations within Tarantool for applications like asynctnt. ```Lua 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) ``` -------------------------------- ### Basic Tarantool Interaction with asynctnt (Lua & Python) Source: https://github.com/igorcoding/asynctnt/blob/main/docs/examples.md This section demonstrates a fundamental interaction with Tarantool. The Lua code initializes a Tarantool instance, creates a 'tester' space, and defines its schema. The Python code then connects to this Tarantool instance using asynctnt, inserts data, performs a select operation, and prints the results, showcasing basic CRUD operations and data access. ```lua 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 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()) ``` -------------------------------- ### Tarantool SQL Configuration for asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/docs/overview.md This Lua snippet configures a Tarantool instance to listen on a specific address and port, grants read/write/execute permissions to the 'guest' user, and creates a 'users' table with 'id' and 'name' columns. This setup is essential for `asynctnt` to interact with the database. ```lua 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) ``` -------------------------------- ### Basic asynctnt Usage: Connect, Insert, and Select Data Source: https://github.com/igorcoding/asynctnt/blob/main/README.md This Python asyncio example demonstrates how to connect to a Tarantool instance using asynctnt, insert multiple records into the 'tester' space, and then select all data. It also shows how to access fields in the returned TarantoolTuple by both index and name, as seen in the console output. ```Python 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()) ``` -------------------------------- ### Connect to Tarantool and perform DML operations with asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/docs/overview.md This Python asyncio example demonstrates how to establish a connection to a Tarantool database using asynctnt, insert multiple records into the 'tester' space, and then select all records. It highlights asynctnt's ability to access fields of the returned TarantoolTuple by both numerical index and field name, leveraging the automatic schema fetching feature. ```python 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()) ``` -------------------------------- ### Manually Control Prepared Statements in asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/docs/sql.md Provides an example of explicitly calling `stmt.prepare()` and `stmt.unprepare()` methods to control the lifecycle of a prepared statement, offering more granular control than the context manager. ```python stmt = conn.prepare('select id, name from users where id = ?') await stmt.prepare() user1 = stmt.execute([1]) assert user1.name == 'James Bond' user2 = stmt.execute([2]) assert user2.name == 'Ethan Hunt' await stmt.unprepare() ``` -------------------------------- ### Managing asynctnt Connections with Context Manager Source: https://github.com/igorcoding/asynctnt/blob/main/docs/examples.md This Python snippet demonstrates how to use asynctnt.Connection as an asynchronous context manager. This pattern ensures that the connection is properly opened and closed, even if errors occur. It connects to Tarantool and calls the box.info stored procedure to retrieve instance information. ```python import asyncio import asynctnt async def main(): async with asynctnt.Connection(port=3301) as conn: res = await conn.call('box.info') print(res.body) asyncio.run(main()) ``` -------------------------------- ### Python Client Receiving Tarantool Pushes and Return Value Source: https://github.com/igorcoding/asynctnt/blob/main/docs/pushes.md This Python snippet extends the previous example to demonstrate how to receive both the session push notifications and the final return value from the Tarantool function call. After iterating through all push messages, the code awaits the original future to get the function's return. ```Python import asyncio import asynctnt async def main(): async with asynctnt.Connection(port=3301) as conn: fut = conn.call('sub', [10], push_subscribe=True) it = asynctnt.PushIterator(fut) async for value in it: print(value) print(await fut) # receive the response asyncio.run(main()) ``` -------------------------------- ### Insert Data with Extended Types using asynctnt (Python) Source: https://github.com/igorcoding/asynctnt/blob/main/docs/mpext.md This Python example shows how to connect to Tarantool using `asynctnt` and insert a record into the 'wallets' space. It demonstrates passing Python `UUID`, `Decimal`, and `datetime` objects, which `asynctnt` correctly encodes for Tarantool. ```python import pytz import datetime import uuid import asynctnt from decimal import Decimal Moscow = pytz.timezone('Europe/Moscow') conn = await asynctnt.connect() await conn.insert('wallets', { 'id': 1, 'uuid': uuid.uuid4(), 'money': Decimal('42.17'), 'created_at': datetime.datetime.now(tz=Moscow) }) ``` -------------------------------- ### Python Client Receiving Tarantool Session Pushes Source: https://github.com/igorcoding/asynctnt/blob/main/docs/pushes.md This Python example shows how to subscribe to and receive session push notifications from a Tarantool server using `asynctnt`. It calls the `sub` function on the server, subscribes to pushes, and then iterates through the received messages using `PushIterator`. ```Python import asyncio import asynctnt async def main(): async with asynctnt.Connection(port=3301) as conn: fut = conn.call('sub', [10], push_subscribe=True) it = asynctnt.PushIterator(fut) async for value in it: print(value) asyncio.run(main()) ``` -------------------------------- ### Run asynctnt test suite Source: https://github.com/igorcoding/asynctnt/blob/main/docs/installation.md Instructions to execute the test suite for the asynctnt project. This command typically runs predefined tests to ensure the library's functionality. ```bash $ make test ``` -------------------------------- ### Configure Tarantool for SQL with asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/docs/sql.md Sets up a Tarantool instance with a listener on a specific port, grants necessary permissions to the 'guest' user, and creates a 'users' table with an auto-incrementing primary key for SQL operations. ```lua 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 autoincrement, name text ) ]]) end) ``` -------------------------------- ### Use Prepared Statements with Context Manager in asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/docs/sql.md Shows how to utilize prepared statements for optimized execution of repeated SQL queries. The `async with` context manager automatically handles the `prepare()` and `unprepare()` calls, ensuring efficient resource management. ```python stmt = conn.prepare('select id, name from users where id = ?') async with stmt: user1 = stmt.execute([1]) assert user1.name == 'James Bond' user2 = stmt.execute([2]) assert user2.name == 'Ethan Hunt' ``` -------------------------------- ### asynctnt Performance Benchmarks Source: https://github.com/igorcoding/asynctnt/blob/main/docs/overview.md This section presents performance benchmarks for `asynctnt` operations, including `ping`, `call`, `eval`, `select`, `insert`, `update`, and `execute`. Tests were conducted sequentially and in parallel, with and without `uvloop` enabled, highlighting the significant performance improvement with `uvloop`. ```APIDOC Benchmark Environment: CPU: 2 GHz Quad-Core Intel Core i5 Memory: 16GB 3733 MHz LPDDR4X Tarantool Configuration: wal_mode = 'none' Performance Metrics (Requests/second): Operation | 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 ``` -------------------------------- ### Using asynctnt Streams for Basic Transactions Source: https://github.com/igorcoding/asynctnt/blob/main/docs/streams.md Demonstrates the basic usage of streams in `asynctnt` for performing transactional operations like insert and update. The `async with conn.stream()` context manager automatically handles `begin()`, `commit()`, and `rollback()` if an exception occurs. ```python import asynctnt conn = await asynctnt.connect() async with conn.stream() as s: data = [1, 'Peter Parker'] await s.insert('heroes', data) await s.update('heroes', [1], ['=', 'name', 'Spider-Man']) res = await conn.select('heroes') print(res) ``` -------------------------------- ### asynctnt Python: Executing SQL Queries Source: https://github.com/igorcoding/asynctnt/blob/main/README.md This Python snippet demonstrates connecting to Tarantool using `asynctnt` and performing SQL `INSERT` and `SELECT` operations. It shows how to use parameterized queries for data insertion and iterate over results, highlighting basic data manipulation. ```Python 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()) ``` -------------------------------- ### Perform Basic SQL Operations with asynctnt in Python Source: https://github.com/igorcoding/asynctnt/blob/main/docs/sql.md Demonstrates connecting to a Tarantool instance, inserting data into the 'users' table using both positional and named parameters, retrieving auto-incremented primary keys, and selecting all data to iterate through rows. ```python 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 (name) values (?)", ['James Bond']) resp = await conn.execute("insert into users (name) values (:name)", [{':name', 'Ethan Hunt'}]) # get value of auto incremented primary key print(resp.autoincrement_ids) data = await conn.execute('select * from users') for row in data: print(row) await conn.disconnect() asyncio.run(main()) ``` -------------------------------- ### Flexible Transaction Management in asynctnt with Tarantool Source: https://github.com/igorcoding/asynctnt/blob/main/docs/streams.md Illustrates various methods to initiate, commit, and rollback transactions in Tarantool using `asynctnt`. It includes `asynctnt`'s native methods, `box.call` for stored procedures, and direct SQL `execute` commands, offering flexibility in transaction control. ```python # begin() variants await conn.begin() await conn.call('box.begin') await conn.execute('START TRANSACTION') # commit() variants await conn.commit() await conn.call('box.commit') await conn.execute('COMMIT') # rollback() variants await conn.rollback() await conn.call('box.rollback') await conn.execute('ROLLBACK') ``` -------------------------------- ### Use Named Parameters in Prepared Statements with asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/docs/sql.md Demonstrates how to use named parameters (e.g., ':id') within prepared statements, which can improve readability and maintainability of SQL queries, especially when dealing with multiple parameters. ```python stmt = conn.prepare('select id, name from users where id = :id') async with stmt: user1 = stmt.execute([ {':id': 1} ]) assert user1.name == 'James Bond' user2 = stmt.execute([ {':id': 2} ]) assert user2.name == 'Ethan Hunt' ``` -------------------------------- ### Configure Tarantool Instance for asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/README.md This Lua snippet configures a Tarantool instance, setting up a listener, granting permissions, and defining a 'tester' space with a primary index and specific field formats (id, name, uuid) for use with asynctnt. ```Lua 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) ``` -------------------------------- ### Tarantool Performance Configuration for Benchmarks Source: https://github.com/igorcoding/asynctnt/blob/main/docs/overview.md This Lua snippet configures Tarantool's `wal_mode` to 'none', which is a common setting for performance benchmarks to minimize disk I/O overhead. This configuration is used for the performance tests presented in the document. ```lua box.cfg{wal_mode = 'none'} ``` -------------------------------- ### Tarantool Lua: Performance Benchmark Configuration Source: https://github.com/igorcoding/asynctnt/blob/main/README.md This Lua snippet configures Tarantool's `wal_mode` to 'none'. This setting is crucial for performance testing as it disables write-ahead logging, reducing I/O overhead and allowing for higher transaction rates during benchmarks. ```Lua box.cfg{wal_mode = 'none'} ``` -------------------------------- ### asynctnt Python SQL Operations Source: https://github.com/igorcoding/asynctnt/blob/main/docs/overview.md This Python snippet demonstrates connecting to a Tarantool instance using `asynctnt`, inserting data into the 'users' table, and then selecting all records. It showcases basic CRUD operations and how to iterate through the returned data. The connection is properly closed after operations. The expected output for this code is: ```python 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()) ``` -------------------------------- ### Access SQL Query Metadata with asynctnt in Python Source: https://github.com/igorcoding/asynctnt/blob/main/docs/sql.md Illustrates how to access and verify metadata associated with an SQL query result, such as field names and types, through the `res.metadata` attribute of the response object. ```python res = await conn.execute("select * from users") assert res.metadata.fields[0].name == 'ID' assert res.metadata.fields[0].type == 'integer' assert res.metadata.fields[1].name == 'NAME' assert res.metadata.fields[0].type == 'string' ``` -------------------------------- ### Accessing Tarantool Space Metadata in Python Source: https://github.com/igorcoding/asynctnt/blob/main/docs/metadata.md This Python snippet demonstrates how to access the fetched Tarantool schema metadata through the `asynctnt.Connection` object. It shows how to retrieve properties like space ID, engine type, and format fields for a specific space, such as the `_space` system space. ```python import asynctnt conn = await asynctnt.connect() print('space id', conn.schema.spaces['_space'].sid) print('space engine', conn.schema.spaces['_space'].engine) print('space format fields', conn.schema.spaces['_space'].metadata.fields) ``` -------------------------------- ### Define Tarantool Schema with Extended Types (Lua) Source: https://github.com/igorcoding/asynctnt/blob/main/docs/mpext.md This Lua snippet demonstrates how to create a Tarantool space named 'wallets' and define its format to include columns for `uuid`, `decimal`, and `datetime` types, along with an `unsigned` integer ID. ```lua local s = box.schema.create_space('wallets') s:format({ { type = 'unsigned', name = 'id' }, { type = 'uuid', name = 'uuid' }, { type = 'decimal', name = 'money' }, { type = 'datetime', name = 'created_at' } }) s:create_index('primary') ``` -------------------------------- ### Tarantool Lua Function for Session Push Source: https://github.com/igorcoding/asynctnt/blob/main/docs/pushes.md This Lua function demonstrates how to send 'n' out-of-bound push messages from Tarantool to a connected client. It iterates 'n' times, pushing 'i' and 'i*i' for each iteration, and then returns the string 'done'. ```Lua function sub(n) for i=1,n do box.session.push(i, i * i) end return 'done' end ``` -------------------------------- ### Controlling Transaction Isolation Level in asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/docs/streams.md Shows how to manually control the isolation level of a transaction using `asynctnt`. It explicitly calls `begin()` with `Isolation.READ_COMMITTED` and `commit()` after operations, demonstrating how to manage transaction boundaries manually. ```python import asynctnt from asynctnt.api import Isolation conn = await asynctnt.connect() s = conn.stream() await s.begin(Isolation.READ_COMMITTED) data = [1, 'Peter Parker'] await s.insert('heroes', data) await s.update('heroes', [1], ['=', 'name', 'Spider-Man']) await s.commit() res = await conn.select('heroes') print(res) ``` -------------------------------- ### Work with Tarantool Interval Type in Python using asynctnt Source: https://github.com/igorcoding/asynctnt/blob/main/docs/mpext.md This Python snippet illustrates how `asynctnt` supports Tarantool's interval type. It uses `conn.eval` to create an interval in Lua and then asserts that the returned value is correctly decoded into an `asynctnt.MPInterval` object in Python, demonstrating its usage as both return type and parameter. ```python import asynctnt async with asynctnt.Connection() as conn: resp = await conn.eval(""" local datetime = require('datetime') return datetime.interval.new({ year=1, month=2, week=3, day=4, hour=5, min=6, sec=7, nsec=8, }) """) assert resp[0] == asynctnt.MPInterval( year=1, month=2, week=3, day=4, hour=5, min=6, sec=7, nsec=8, ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.