Python
Examples on GitHub: sample_db, python
tarantool-python is the official Python connector for Tarantool. It is not supplied as part of the Tarantool repository and must be installed separately.
The tutorial shows how to use the tarantool-python library to create a Python script that connects to a remote Tarantool instance, performs CRUD operations, and executes a stored procedure. You can find the full package documentation here: Python client library for Tarantool.
Примечание
This tutorial shows how to make CRUD requests to a single-instance Tarantool database. To make requests to a sharded Tarantool cluster with the CRUD module, use the tarantool.crud module’s API.
This section describes the configuration of a sample database that allows remote connections:
credentials:
users:
sampleuser:
password: '123456'
privileges:
- permissions: [ read, write ]
spaces: [ bands ]
- permissions: [ execute ]
functions: [ get_bands_older_than ]
groups:
group001:
replicasets:
replicaset001:
instances:
instance001:
iproto:
listen:
- uri: '127.0.0.1:3301'
app:
file: 'myapp.lua'
- The configuration contains one instance that listens for incoming requests on the
127.0.0.1:3301
address. sampleuser
has privileges to select and modify data in thebands
space and execute theget_bands_older_than
stored function. This user can be used to connect to the instance remotely.myapp.lua
defines the data model and a stored function.
The myapp.lua
file looks as follows:
-- Create a space --
box.schema.space.create('bands')
-- Specify field names and types --
box.space.bands:format({
{ name = 'id', type = 'unsigned' },
{ name = 'band_name', type = 'string' },
{ name = 'year', type = 'unsigned' }
})
-- Create indexes --
box.space.bands:create_index('primary', { parts = { 'id' } })
box.space.bands:create_index('band', { parts = { 'band_name' } })
box.space.bands:create_index('year_band', { parts = { { 'year' }, { 'band_name' } } })
-- Create a stored function --
box.schema.func.create('get_bands_older_than', {
body = [[
function(year)
return box.space.bands.index.year_band:select({ year }, { iterator = 'LT', limit = 10 })
end
]]
})
You can find the full example on GitHub: sample_db.
Before creating and starting a client Python application, you need to run the sample_db application using tt start:
$ tt start sample_db
Now you can create a client Python application that makes requests to this database.
Before you start, make sure you have Python installed on your computer.
Create the
hello
directory for your application and go to this directory:$ mkdir hello $ cd hello
Create and activate a Python virtual environment:
$ python -m venv .venv $ source .venv/bin/activate
Install the
tarantool
module:$ pip install tarantool
Inside the
hello
directory, create thehello.py
file for application code.
Add the following code:
# Connect to the database
conn = tarantool.Connection(host='127.0.0.1',
port=3301,
user='sampleuser',
password='123456')
This code establishes a connection to a running Tarantool instance on behalf of sampleuser
.
The conn
object can be used to make CRUD requests and execute stored procedures.
Add the following code to insert four tuples into the bands
space:
# Insert data
tuples = [(1, 'Roxette', 1986),
(2, 'Scorpions', 1965),
(3, 'Ace of Base', 1987),
(4, 'The Beatles', 1960)]
print("Inserted tuples:")
for tuple in tuples:
response = conn.insert(space_name='bands', values=tuple)
print(response[0])
Connection.insert()
is used to insert a tuple to the space.
To get a tuple by the specified primary key value, use Connection.select()
:
# Select by primary key
response = conn.select(space_name='bands', key=1)
print('Tuple selected by the primary key value:', response[0])
You can also get a tuple by the value of the specified index using the index
argument:
# Select by secondary key
response = conn.select(space_name='bands', key='The Beatles', index='band')
print('Tuple selected by the secondary key value:', response[0])
Connection.update()
can be used to update a tuple identified by the primary key as follows:
# Update
response = conn.update(space_name='bands',
key=2,
op_list=[('=', 'band_name', 'Pink Floyd')])
print('Updated tuple:', response[0])
Connection.upsert()
updates an existing tuple or inserts a new one.
In the example below, a new tuple is inserted:
# Upsert
conn.upsert(space_name='bands',
tuple_value=(5, 'The Rolling Stones', 1962),
op_list=[('=', 'band_name', 'The Doors')])
In this example, Connection.replace()
deletes the existing tuple and inserts a new one:
# Replace
response = conn.replace(space_name='bands', values=(1, 'Queen', 1970))
print('Replaced tuple:', response[0])
To execute a stored procedure, use Connection.call()
:
# Call
response = conn.call('get_bands_older_than', 1966)
print('Stored procedure result:', response[0])
The Connection.close()
method can be used to close the connection when it is no longer needed:
# Close connection
conn.close()
print('Connection is closed')
Примечание
You can find the example with all the requests above on GitHub: python.
To run the resulting application, pass the script name to the python
command:
$ python hello.py
Inserted tuples:
[1, 'Roxette', 1986]
[2, 'Scorpions', 1965]
[3, 'Ace of Base', 1987]
[4, 'The Beatles', 1960]
Tuple selected by the primary key value: [1, 'Roxette', 1986]
Tuple selected by the secondary key value: [4, 'The Beatles', 1960]
Updated tuple: [2, 'Pink Floyd', 1965]
Replaced tuple: [1, 'Queen', 1970]
Deleted tuple: [5, 'The Rolling Stones', 1962]
Stored procedure result: [[2, 'Pink Floyd', 1965], [4, 'The Beatles', 1960]]
Connection is closed
Last update: September 2023
There are also several community-driven Python connectors:
- asynctnt with asyncio support
- aiotarantool also with asyncio support, no active maintenance
- gtarantool with gevent support, no active maintenance
The table below contains a feature comparison for asynctnt
and tarantool-python
.
Parameter | igorcoding/asynctnt | tarantool/tarantool-python |
---|---|---|
License | Apache License 2.0 | BSD-2 |
Is maintained | Yes | Yes |
Known Issues | None | None |
Documentation | Yes (github.io) | Yes (readthedocs and tarantool.io) |
Testing / CI / CD | GitHub Actions | GitHub Actions |
GitHub Stars | 73 | 92 |
Static Analysis | Yes (Flake8) | Yes (Flake8, Pylint) |
Packaging | pip | pip, deb, rpm |
Code coverage | Yes | Yes |
Support asynchronous mode | Yes, asyncio | No |
Batching support | No | Yes (with CRUD API) |
Schema reload | Yes (automatically, see auto_refetch_schema) | Yes (automatically) |
Space / index names | Yes | Yes |
Access tuple fields by names | Yes | No |
SQL support | Yes | Yes |
Interactive transactions | Yes | No (issue #163) |
Varbinary support | Yes (in MP_BIN fields) |
Yes |
Decimal support | Yes | Yes |
UUID support | Yes | Yes |
EXT_ERROR support | Yes | Yes |
Datetime support | Yes | Yes |
Interval support | No (issue #30) | Yes |
box.session.push() responses | Yes | Yes |
Session settings | No | No |
Graceful shutdown | No | No |
IPROTO_ID (feature discovery) | Yes | Yes |
CRUD support | No | Yes |
Transparent request retrying | No | No |
Transparent reconnecting | Autoreconnect | Yes (reconnect_max_attempts, reconnect_delay), checking of connection liveness |
Connection pool | No | Yes (with master discovery) |
Support of PEP 249 – Python Database API Specification v2.0 | No | Yes |
Encrypted connection (Enterprise Edition) | No (issue #22) | Yes |