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.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

coleridge-0.1.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for coleridge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c0c10788d6e4fca71b0f0d41f295c5cb3693a2b857ab3f06af4fe8443c6ded2c
MD5 a6a5ce8c45acf02e13fcf4e34c6cd0aa
BLAKE2b-256 f7edc960a3b080a20789dd2407f1091e0255de22b37d6431eea6b4b0b6aaf622

See more details on using hashes here.

File details

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

File metadata

  • Download URL: coleridge-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for coleridge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87092b639bcefd6d4e76c246c477119642b81fe4dbbe20120ecbf1e9b0aaa944
MD5 93cafcc29cac31789969c6ae9f41d7e9
BLAKE2b-256 7a97b804b8f2e27c462d1cb4290675dc23930ba2f4f05962ad0101fb0f288f2a

See more details on using hashes here.

Supported by

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