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.1.tar.gz
(9.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
coleridge-0.2.1-py3-none-any.whl
(14.3 kB
view details)
File details
Details for the file coleridge-0.2.1.tar.gz.
File metadata
- Download URL: coleridge-0.2.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e82b8e998d2d54fb6fd05047435f0667e284a60c04d0d245d4c762a97b7ede1a
|
|
| MD5 |
d33a7fa95b40b4f09b4c13667d75e66a
|
|
| BLAKE2b-256 |
abe9c174c351bfcad302c9ecf70ceda733329581d43ce2c276afaf19e6639b00
|
File details
Details for the file coleridge-0.2.1-py3-none-any.whl.
File metadata
- Download URL: coleridge-0.2.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1f05f4144f5ee68685cf3759fc1fa7dccd11bea125550ac7f41256b4b1329eb
|
|
| MD5 |
50310c71304278007598fbc78dec6d26
|
|
| BLAKE2b-256 |
75fc3b2dcf80994777a1c51c586b23f1fa7685a2e1d807b30a54a6ed917d1de6
|