-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquickstart.py
More file actions
56 lines (40 loc) · 1.31 KB
/
Copy pathquickstart.py
File metadata and controls
56 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Awa Python quickstart — a complete runnable example.
Requires: pip install awa-pg
Requires: a running Postgres instance with DATABASE_URL set.
Usage:
DATABASE_URL=postgres://localhost/mydb python examples/quickstart.py
"""
import asyncio
import os
from dataclasses import dataclass
import awa
DATABASE_URL = os.environ.get(
"DATABASE_URL", "postgres://postgres:test@localhost:15432/awa_test"
)
@dataclass
class SendEmail:
to: str
subject: str
async def main():
client = awa.AsyncClient(DATABASE_URL)
await client.migrate()
# Define a worker
@client.task(SendEmail, queue="email")
async def handle_email(job):
print(f"Sending email to {job.args.to}: {job.args.subject}")
# Start processing before the first enqueue so a fresh 0.6 database can
# auto-finalize to the queue-storage engine.
await client.start([("email", 2)])
# Insert a job
job = await client.insert(
SendEmail(to="alice@example.com", subject="Welcome"),
queue="email",
)
print(f"Inserted job {job.id} (kind={job.kind}, state={job.state})")
await asyncio.sleep(1)
await client.shutdown()
# Verify it completed
result = await client.get_job(job.id)
print(f"Job {result.id} state: {result.state}")
if __name__ == "__main__":
asyncio.run(main())