Skip to main content
Badges:

license pyversions status pypiversion

Master CI:

travis coverage

Downloads:

http://pypi.python.org/pypi/pulsar-odm

Source:

https://github.com/quantmind/pulsar-odm

Mailing list:

google user group

Design by:

Quantmind and Luca Sbardella

Platforms:

Linux, OSX, Windows. Python 3.5 and above

Keywords:

sql, sqlalchemy, asynchronous, asyncio, concurrency, greenlet

Pulsar-odm is build on top of pulsar, sqlalchemy and greenlet libraries to provide an implicit asynchronous object data mapper to use with code written with asyncio. Currently only one dialect is implemented and tested:

Usage

To be able to use the object data mapper within standard blocking code, one needs to use pulsar GreenPool as the following snippet highlights:

from datetime import datetime

from sqlalchemy import Integer, Column, String, DateTime, Boolean

from pulsar.apps.greenio import GreenPool

import odm


class Task(odm.Model):
    id = Column(Integer, primary_key=True)
    subject = Column(String(250))
    done = Column(Boolean, default=False)
    created = Column(DateTime, default=datetime.utcnow)

    def __str__(self):
        return '%d: %s' % (self.id, self.subject)


def example(mp):
    # Make sure table is available
    mp.table_create()
    # Insert a new Task in the table
    with mp.begin() as session:
        task = mp.task(subject='my task')
        session.add(task)
    return task


if __name__ == '__main__':
    pool = GreenPool()
    mp = odm.Mapper('postgresql+green://odm:odmtest@127.0.0.1:5432/odmtests')
    mp.register(Task)
    task = pool._loop.run_until_complete(pool.submit(example, mp))
    print(task)

The example function is executed in a greenlet other than the main one. This is important otherwise the call fails:

>> example(mp)
>> Traceback (most recent call last):
...
RuntimeError: acquire in main greenlet

Running the function on the greenlet pool guarantees the correct asynchronous execution. When psycopg2 executes a command against the database on a child greenlet, it switches control to the parent (main) greenlet, which is controlled by the asyncio eventloop so that other asynchronous operations can be carried out. Once the result of the execution is ready, the execution switches back to the original child greenlet so that the example function can continue.

Testing

To run tests, create a new role and database, for postgresql:

psql -a -f tests/db.sql

Changelog

Release files for pulsar-odm 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pulsar-odm 0.7.0
File Size Uploaded
pulsar-odm-0.7.0.tar.gz 40.2 kB Details

Release files / pulsar-odm-0.7.0.tar.gz

Download URL pulsar-odm-0.7.0.tar.gz
Size 40.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5a1bef53f7d8b85e91fdbf10c103d9fd2cadaf8549a182c193a150c997c9fdb3
BLAKE2b-256 checksum
How to use checksums
c9a277781f094fa4fac04526e7d6f673b4b8d5e3b7b5f0ce544f15deb0c7ca06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.7.0 This release

1 release file

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page