-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
55 lines (42 loc) · 1.57 KB
/
Copy pathtest.py
File metadata and controls
55 lines (42 loc) · 1.57 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
import asyncio
import logging
import uuid
from time import sleep
from protollm_sdk.object_interface.result_storage import RedisResultStorage, JobStatusType
async def correct_job_lifecycle(job_id: str, rrs: RedisResultStorage):
"""
This function simulates a work lifecycle by sleeping for 1 second.
"""
rrs.create_job_status(job_id)
job = rrs.get_job_status(job_id)
print(f"Job {job_id} status: {job.status}")
sleep(1)
rrs.update_job_status(job_id, JobStatusType.IN_PROGRESS)
job = rrs.get_job_status(job_id)
print(f"Job {job_id} status: {job.status}")
sleep(1)
rrs.complete_job(job_id, result="Success")
job = rrs.get_job_status(job_id)
print(f"Job {job_id} status: {job.status}")
async def job_finish_subscriber(job_id: str, rrs: RedisResultStorage):
"""
This function subscribes to job status updates.
"""
sleep(5)
job = await rrs.wait_for_completion(job_id)
print(f"Job {job_id} completed with result: {job.result}")
async def subscriber(job_id: str, rrs: RedisResultStorage):
ans = ""
for job_status in rrs.subscribe(job_id):
ans += f"{job_status.value}\n"
print(f"Subscribed to {job_id} with result: {ans}")
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
job_id = str(uuid.uuid4())
rrs = RedisResultStorage(redis_host="localhost", redis_port=6380)
# add two tasks asynchronously
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(
correct_job_lifecycle(job_id, rrs),
job_finish_subscriber(job_id, rrs)
))