-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpu_worker.py
More file actions
70 lines (50 loc) · 1.87 KB
/
Copy pathgpu_worker.py
File metadata and controls
70 lines (50 loc) · 1.87 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gc
import threading
from queue import Queue
from utils.embedding_utils import embed_batch
from utils.mongo_utils import store_embeddings_in_db, vector_collection
# Internal embedding queue (shared with processor)
embedding_queue = Queue(maxsize=20000)
# Signal for clean shutdown
STOP_SIGNAL = object()
def gpu_worker():
print("[Info] Internal GPU worker started")
while True:
task = embedding_queue.get()
if task is STOP_SIGNAL:
print("[Info] GPU worker stopping")
break
chunks, document_name, tender_id, is_last_batch = task
try:
# Add metadata to each chunk (same as original GPU server)
for c in chunks:
c["tender_id"] = tender_id
c["document_name"] = document_name
# Perform embedding
embeddings = embed_batch(chunks)
# Store embedding vectors in Mongo
store_embeddings_in_db(embeddings, document_name, tender_id)
# Mark document complete if this is the final batch
if is_last_batch:
vector_collection.update_one(
{"tender_id": tender_id, "document_name": document_name},
{"$set": {"document_complete": True}},
upsert=True,
)
except Exception as e:
print(f"[GPU WORKER] Error processing {document_name}: {e}")
gc.collect()
embedding_queue.task_done()
def start_gpu_worker():
"""
Spawns the GPU worker thread in daemon mode.
Must be called once at program startup (e.g. in python_worker.py).
"""
thread = threading.Thread(target=gpu_worker, daemon=True)
thread.start()
print("[Info] GPU worker thread started")
return thread
def stop_gpu_worker():
embedding_queue.put(STOP_SIGNAL)
def join_gpu_worker(thread):
thread.join()