Forked from aioodbc and make improvement
Project description
asyncodbc is a Python 3.5+ module that makes it possible to access ODBC databases with asyncio. It relies on the awesome pyodbc library and preserves the same look and feel. asyncodbc was written using async/await syntax (PEP492) and thus is not compatible with Python versions older than 3.5. Internally asyncodbc employs threads to avoid blocking the event loop, threads are not that as bad as you think!. Other drivers like motor use the same approach.
asyncodbc is fully compatible and tested with uvloop. Take a look at the test suite, all tests are executed with both the default event loop and uvloop.
Supported Databases
asyncodbc should work with all databases supported by pyodbc. But for now the library has been tested with: SQLite, MySQL and PostgreSQL. Feel free to add other databases to the test suite by submitting a PR.
Basic Example
asyncodbc is based on pyodbc and provides the same api, you just need to use yield from conn.f() or await conn.f() instead of conn.f()
Properties are unchanged, so conn.prop is correct as well as conn.prop = val.
import asyncio
import asyncodbc
loop = asyncio.get_event_loop()
async def test_example():
dsn = 'Driver=SQLite;Database=sqlite.db'
conn = await asyncodbc.connect(dsn=dsn, loop=loop)
cur = await conn.cursor()
await cur.execute("SELECT 42 AS age;")
rows = await cur.fetchall()
print(rows)
print(rows[0])
print(rows[0].age)
await cur.close()
await conn.close()
loop.run_until_complete(test_example())
Connection Pool
Connection pooling is ported from aiopg and relies on PEP492 features:
import asyncio
import asyncodbc
loop = asyncio.get_event_loop()
async def test_pool():
dsn = 'Driver=SQLite;Database=sqlite.db'
pool = await asyncodbc.create_pool(dsn=dsn, loop=loop)
async with pool.acquire() as conn:
cur = await conn.cursor()
await cur.execute("SELECT 42;")
r = await cur.fetchall()
print(r)
await cur.close()
await conn.close()
pool.close()
await pool.wait_closed()
loop.run_until_complete(test_pool())
Context Managers
Pool, Connection and Cursor objects support the context management protocol:
import asyncio
import asyncodbc
loop = asyncio.get_event_loop()
async def test_example():
dsn = 'Driver=SQLite;Database=sqlite.db'
async with asyncodbc.create_pool(dsn=dsn, loop=loop) as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute('SELECT 42 AS age;')
val = await cur.fetchone()
print(val)
print(val.age)
loop.run_until_complete(test_example())
Installation
In a linux environment pyodbc (hence asyncodbc) requires the unixODBC library. You can install it using your package manager, for example:
$ sudo apt-get install unixodbc $ sudo apt-get install unixodbc-dev
then:
pip install asyncodbc
Run tests
For testing purposes you need to install docker and the development requirements:
$ pip install -r requirements-dev.txt
In order to simplify development you should install the provided docker container. This way you don’t need to install any databases or other system libraries, everything happens inside the container.
Then just execute:
$ make docker_build $ make docker_test
The test will automatically pull images and build containers with the required databases.
NOTE: Running tests requires Python 3.6 or higher.
Other SQL Drivers
Requirements
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file asyncodbc-0.1.1.tar.gz
.
File metadata
- Download URL: asyncodbc-0.1.1.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | feb0d4bb35e028d37d914b1ddcbd576400be6058bf256bc226793c966f846d54 |
|
MD5 | 530783b8ac0eb80c18a3763e53642480 |
|
BLAKE2b-256 | 6f0b22492b37913e1e85806cf760fad88f261df1492a96677f8c99e9af57d9bb |
File details
Details for the file asyncodbc-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: asyncodbc-0.1.1-py3-none-any.whl
- Upload date:
- Size: 22.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce1ba96b4b5b6b37d6c2134c435172c91f05c15c7ce1ec830be2806b6e545442 |
|
MD5 | fdb1fc8bc6d1f3628f5cd4d08b42fa4d |
|
BLAKE2b-256 | b96fd9759613546f57611bde94d4fb96d494d2132f937b0de2acc9eb42edcb4e |