-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_queue.py
45 lines (38 loc) · 1.05 KB
/
sync_queue.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
import threading
from datetime import datetime
class SyncQueue:
def __init__(self):
self._lock = threading.Lock()
self._queue = []
self._last_added = datetime.now()
def put(self, item):
now = datetime.now()
self._lock.acquire()
self._queue.append(item)
self._last_added = max(now, self._last_added)
self._lock.release()
def get(self):
self._lock.acquire()
if len(self._queue) > 0:
item = self._queue.pop(0)
self._lock.release()
return item
self._lock.release()
return None
@property
def last_added(self) -> datetime:
self._lock.acquire()
ans = self._last_added
self._lock.release()
return ans
@property
def empty(self) -> bool:
self._lock.acquire()
ans = len(self._queue) == 0
self._lock.release()
return ans
def __len__(self) -> int:
self._lock.acquire()
ans = len(self._queue)
self._lock.release()
return ans