Skip to main content

No project description provided

Project description

Coleridge

Use Pydantic with RabbitMQ or to run background tasks.

Example

from time import sleep
from pydantic import BaseModel
from coleridge import Coleridge, Empty, Connection

class Poem(BaseModel):
    lines: List[str]


# Background task
coleridge = Coleridge()

@coleridge
def long_task(_: Empty) -> Poem:
    poem = Poem(lines=["In Xanadu did Kubla Khan",
            "A stately pleasure-dome decree:",])
    sleep(5)
    return poem

def print_poem(poem: Poem) -> None:
    for line in poem.lines:
        print(f"~~ {line} ~~")

def print_error(error: Exception) -> None:
    print(error)

# Assing a function to run when the exection finishes
long_task.on_finish = print_poem

# Or catch errors
long_task.on_error = print_error

# Run the task
long_task.run(Empty()) # It always requires a pydantic model, empty in this case


# Or use RabbitMQ instead of background functions
rabbit = Coleridge(Connection(host="localhost", port=5672))

@rabbit
def long_task_with_rabbit(poem: Poem) -> Empty:
    for line in poem.lines:
        print(f"~~ {line} ~~")
        sleep(5)
    return Empty()

long_task_with_rabbit.run(Poem(lines=["In Xanadu did Kubla Khan",
            "A stately pleasure-dome decree:",]))

You can also call the results directly like this:

from time import sleep
from pydantic import BaseModel
from coleridge import Coleridge, Empty, Connection

class Poem(BaseModel):
    lines: List[str]


# Background task
coleridge = Coleridge()

@coleridge
def long_task(_: Empty) -> Poem:
    poem = Poem(lines=["In Xanadu did Kubla Khan",
            "A stately pleasure-dome decree:",])
    sleep(5)
    return poem

result = long_task.run(Empty())

while True:
    if result.finished:
        if result.success:
            print(result.result)
        elif result.error:
            print(result.error)
        break

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

coleridge-0.2.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

coleridge-0.2.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file coleridge-0.2.0.tar.gz.

File metadata

  • Download URL: coleridge-0.2.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for coleridge-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3ff427d68230147364462b587a652ebead1b649966e50ddd961c9fa954eba6ea
MD5 f15574cf0142d6b97bff7dc886acf509
BLAKE2b-256 9cdcb701589d0028cf84599ed41c32257cfe09b8b6a046cc1ee456997c63f2de

See more details on using hashes here.

File details

Details for the file coleridge-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: coleridge-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for coleridge-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fd7955aae405783fd509629b5aa4f43bf4bd327e2a878b91fa803205a936aae
MD5 a8662824d68dfb981384ad56b40ac188
BLAKE2b-256 20fb09987208ecfc9f6f10a6cd42efdd7545e0ee33f42bc0b58c71697275bb58

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