Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/freonius/coleridge
A clean RabbitMQ decorator
https://github.com/freonius/coleridge
Last synced: 10 days ago
JSON representation
A clean RabbitMQ decorator
- Host: GitHub
- URL: https://github.com/freonius/coleridge
- Owner: Freonius
- License: mit
- Created: 2024-08-20T07:06:32.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-08-29T06:23:03.000Z (3 months ago)
- Last Synced: 2024-08-29T07:55:34.657Z (3 months ago)
- Language: Python
- Size: 66.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/funding.yml
- License: LICENSE
Awesome Lists containing this project
README
# Coleridge
Use Pydantic with RabbitMQ or to run background tasks.
## Example
```python
from time import sleep
from pydantic import BaseModel
from coleridge import Coleridge, Empty, Connectionclass 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 poemdef 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:
```python
from time import sleep
from pydantic import BaseModel
from coleridge import Coleridge, Empty, Connectionclass 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 poemresult = long_task.run(Empty())
while True:
if result.finished:
if result.success:
print(result.result)
elif result.error:
print(result.error)
break
```