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.
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
hellodirectory 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
tarantoolmodule:$ pip install tarantool -
Inside the
hellodirectory, create thehello.pyfile for application code.
In the hello.py file, import the tarantool package:
import tarantool# Connect to the databaseconn = tarantool.Connection(host='127.0.0.1',port=3301,user='sampleuser',password='123456')# Insert datatuples = [(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])# Select by primary keyresponse = conn.select(space_name='bands', key=1)print('Tuple selected by the primary key value:', response[0])# Select by secondary keyresponse = conn.select(space_name='bands', key='The Beatles', index='band')print('Tuple selected by the secondary key value:', response[0])# Updateresponse = conn.update(space_name='bands',key=2,op_list=[('=', 'band_name', 'Pink Floyd')])print('Updated tuple:', response[0])# Upsertconn.upsert(space_name='bands',tuple_value=(5, 'The Rolling Stones', 1962),op_list=[('=', 'band_name', 'The Doors')])# Replaceresponse = conn.replace(space_name='bands', values=(1, 'Queen', 1970))print('Replaced tuple:', response[0])# Deleteresponse = conn.delete(space_name='bands', key=5)print('Deleted tuple:', response[0])# Callresponse = conn.call('get_bands_older_than', 1966)print('Stored procedure result:', response[0])# Close connectionconn.close()print('Connection is closed')
Add the following code:
# Connect to the databaseconn = 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 datatuples = [(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 keyresponse = 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 keyresponse = 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:
# Updateresponse = 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:
# Upsertconn.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:
# Replaceresponse = conn.replace(space_name='bands', values=(1, 'Queen', 1970))print('Replaced tuple:', response[0])
Connection.delete() in the example below deletes a tuple whose primary
key value is 5:
# Deleteresponse = conn.delete(space_name='bands', key=5)print('Deleted tuple:', response[0])
To execute a stored procedure, use Connection.call():
# Callresponse = 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 connectionconn.close()print('Connection is closed')
To run the resulting application, pass the script name to the python
command:
$ python hello.pyInserted 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