This repository was archived by the owner on Mar 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_handler.py
More file actions
73 lines (59 loc) · 2.1 KB
/
Copy pathqueue_handler.py
File metadata and controls
73 lines (59 loc) · 2.1 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
71
72
73
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import tempfile
import pickle
import threading
from typing import Any, Optional
class SharedQueue:
"""Thread-safe queue using file system for IPC."""
def __init__(self, name: str = 'anj_dev_queue'):
"""Initialize queue."""
self.name = name
self.queue_file = os.path.join(tempfile.gettempdir(), f"{name}.queue")
self.lock = threading.Lock()
self._initialize()
def _initialize(self):
"""Create queue file if it doesn't exist."""
if not os.path.exists(self.queue_file):
with open(self.queue_file, 'wb') as f:
pickle.dump([], f)
def put(self, item: Any):
"""Add item to queue."""
with self.lock:
try:
with open(self.queue_file, 'rb') as f:
items = pickle.load(f)
except:
items = []
items.append(item)
with open(self.queue_file, 'wb') as f:
pickle.dump(items, f)
def get(self) -> Optional[Any]:
"""Get next item from queue."""
with self.lock:
try:
with open(self.queue_file, 'rb') as f:
items = pickle.load(f)
if not items:
return None
item = items.pop(0)
with open(self.queue_file, 'wb') as f:
pickle.dump(items, f)
return item
except:
return None
def clear(self):
"""Clear the queue."""
with self.lock:
with open(self.queue_file, 'wb') as f:
pickle.dump([], f)
def __del__(self):
"""Clean up queue file."""
try:
if os.path.exists(self.queue_file):
os.remove(self.queue_file)
except:
pass
# Global queue instance
log_queue = SharedQueue('anj_dev_logs')