Skip to main content

Object Data Mapper for pulsar asynchronous framework

Project description

Badges:

license pyversions status pypiversion

Master CI:

master-build coverage-master

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pulsar-odm-0.6.1.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pulsar_odm-0.6.1-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file pulsar-odm-0.6.1.tar.gz.

File metadata

  • Download URL: pulsar-odm-0.6.1.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pulsar-odm-0.6.1.tar.gz
Algorithm Hash digest
SHA256 e0e756cf5b37375fb5d78fd352de0b6a0bd2b59478062e5bbd0890ce441025da
MD5 d70c7d4d4e77fbb2e386a5bf19705b5d
BLAKE2b-256 4a6b42580061d7fba8d79707d3d98878658fb67cfabdb909c69779be179e3949

See more details on using hashes here.

File details

Details for the file pulsar_odm-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pulsar_odm-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eff8e492f2453c5a8a36a137b05390886c86929de1b6494a72c5b750e849ffb7
MD5 17aff2b000186df0f1bb8562c9328796
BLAKE2b-256 7b4897b184aac2fd62ce0367d6878833c774de48e293bfc8f68fa5b423061d19

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page