forked from l3uddz/plex_autoscan
-
Notifications
You must be signed in to change notification settings - Fork 5
/
threads.py
66 lines (57 loc) · 1.75 KB
/
threads.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
try:
# Try the Python 3 queue module
import queue
except ImportError:
# Fallback to the Python 2 Queue module
import Queue as queue
import datetime
import copy
import threading
class PriorityLock:
def __init__(self):
self._is_available = True
self._mutex = threading.Lock()
self._waiter_queue = queue.PriorityQueue()
def acquire(self, priority=0):
self._mutex.acquire()
# First, just check the lock.
if self._is_available:
self._is_available = False
self._mutex.release()
return True
event = threading.Event()
self._waiter_queue.put((priority, datetime.datetime.now(), event))
self._mutex.release()
event.wait()
# When the event is triggered, we have the lock.
return True
def release(self):
self._mutex.acquire()
# Notify the next thread in line, if any.
try:
_, timeAdded, event = self._waiter_queue.get_nowait()
except queue.Empty:
self._is_available = True
else:
event.set()
self._mutex.release()
class Thread:
def __init__(self):
self.threads = []
def start(self, target, name=None, args=None, track=False):
thread = threading.Thread(target=target, name=name, args=args if args else [])
thread.daemon = True
thread.start()
if track:
self.threads.append(thread)
return thread
def join(self):
for thread in copy.copy(self.threads):
thread.join()
self.threads.remove(thread)
return
def kill(self):
for thread in copy.copy(self.threads):
thread.kill()
self.threads.remove(thread)
return