-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_queue_worker.py
executable file
·159 lines (130 loc) · 4.85 KB
/
async_queue_worker.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import asyncio
import os
import random
import aio_pika
import pickle
from typing import List
from typing_extensions import Self
COLMAP_QUEUE = "colmap_queue"
TENSORF_QUEUE = "tensorf_queue"
TOTAL_SECONDS = 0
TOTAL_PROCESSED_SECONDS = 0
TOTAL_JOBS_PROCESSED = 0
class QueueWorker:
def __init__(
self,
host: str = "localhost",
port: int = 5672,
queues: List[str] = [COLMAP_QUEUE, TENSORF_QUEUE],
) -> Self:
"""
Unless you really need to modify something, call this method with the default arguments, i.e. QueueServer().
"""
if not queues:
raise ValueError("At least one queue is required")
self.host = host
self.port = port
self.queues = queues
self.url = f"amqp://guest:guest@{self.host}:{self.port}/"
async def connect(self):
self.connection = await aio_pika.connect(host=self.host, port=self.port)
# async with self.connection:
# self.channel = await self.connection.channel()
# await self.channel.set_qos(prefetch_count=1)
# await self.declare()
async def test_queue_worker(self):
"""
Test the QueueWorker class
"""
await self.connect()
async with self.connection:
self.channel = await self.connection.channel()
await self.channel.set_qos(prefetch_count=1)
self.colmap_queue = await self.channel.declare_queue(
name=COLMAP_QUEUE, durable=True
)
print(f" [*] Declared {COLMAP_QUEUE}.")
self.tensorf_queue = await self.channel.declare_queue(
name=TENSORF_QUEUE, durable=True
)
print(f" [*] Declared {TENSORF_QUEUE}.")
await self.colmap_queue.consume(self.colmap_callback)
await self.tensorf_queue.consume(self.tensorf_callback)
print(" [*] Waiting for messages. To exit press CTRL+C")
await asyncio.Future()
async def declare(self):
self.colmap_queue: aio_pika.abc.AbstractQueue = (
await self.channel.declare_queue(name=COLMAP_QUEUE, durable=True)
)
print(f" [*] Declared {COLMAP_QUEUE}.")
self.tensorf_queue: aio_pika.abc.AbstractQueue = (
await self.channel.declare_queue(name=TENSORF_QUEUE, durable=True)
)
print(f" [*] Declared {TENSORF_QUEUE}.")
async def colmap_callback(
self, message: aio_pika.abc.AbstractIncomingMessage
): # demoing as uuid for now
global TOTAL_SECONDS, TOTAL_PROCESSED_SECONDS, TOTAL_JOBS_PROCESSED
# n = random.randint(1, 2)
n = 1
TOTAL_SECONDS += n
print(
f" [x] Sleeping for {n} seconds as colmap process {message.body}\t{TOTAL_PROCESSED_SECONDS}/{TOTAL_SECONDS}"
)
await asyncio.sleep(n)
TOTAL_PROCESSED_SECONDS += n
TOTAL_JOBS_PROCESSED += 1
print(f" [x] Done with colmap process {message.body}")
await message.ack()
if TOTAL_JOBS_PROCESSED == 10:
print(f" [x] Done with all jobs")
os._exit(0)
async def tensorf_callback(
self, message: aio_pika.abc.AbstractIncomingMessage
): # demoing as uuid for now
global TOTAL_SECONDS, TOTAL_PROCESSED_SECONDS, TOTAL_JOBS_PROCESSED
# n = random.randint(1, 2)
n = 1
TOTAL_SECONDS += n
print(
f" [x] Sleeping for {n} seconds as tensorf process {message.body}\t{TOTAL_PROCESSED_SECONDS}/{TOTAL_SECONDS}"
)
await asyncio.sleep(n)
TOTAL_PROCESSED_SECONDS += n
TOTAL_JOBS_PROCESSED += 1
print(f" [x] Done with tensorf process {message.body}")
await message.ack()
if TOTAL_JOBS_PROCESSED == 10:
print(f" [x] Done with all jobs")
os._exit(0)
@staticmethod
def Serialize(data) -> bytes:
"""
Serializes calling object to bytes
Example::
qs = QueueWorker()
data = qs.Serialize("Hello World")
QueueWorker.Serialize("Hello World")
"""
return pickle.dumps(data)
@staticmethod
def Deserialize(data: bytes) -> Self:
"""
Deserializes data to a QueueWorker object, qs2 is the same as qs but they are not the same object under the hood (i.e. their pointers are different, cool right?)
Example::
qw = QueueWorker()
data = qw.Serialize()
qw2 = qw.Deserialize(data)
QueueWorker.Deserialize(data)
"""
return pickle.loads(data)
async def close(self):
"""
Close this object's connection to the RabbitMQ server
"""
await self.connection.close()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
qw = QueueWorker()
loop.run_until_complete(qw.test_queue_worker())