forked from sonya75/NikeAccountGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timedqueue.py
44 lines (44 loc) · 1.11 KB
/
timedqueue.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
from threading import Lock,Condition
import Queue
import time
class TimedQueue:
def __init__(self):
self.itemqueue=Queue.PriorityQueue()
self.getlock=Lock()
self.itemlock=Lock()
self.itemconfirm=Condition(self.itemlock)
def get(self,nowait=False):
self.getlock.acquire()
while True:
self.itemlock.acquire()
if self.itemqueue.qsize()==0:
if nowait:
self.itemlock.release()
self.getlock.release()
raise Exception
self.itemconfirm.wait()
self.itemlock.release()
continue
else:
w=self.itemqueue.get()
z=w[0]-time.time()
if z<=0:
self.itemlock.release()
self.getlock.release()
return w[1]
else:
self.itemqueue.put(w)
if nowait:
self.itemlock.release()
self.getlock.release()
raise Exception
self.itemconfirm.wait(timeout=z)
self.itemlock.release()
continue
def get_nowait(self):
return self.get(True)
def put(self,item,timeafter=0):
self.itemlock.acquire()
self.itemqueue.put((time.time()+timeafter,item))
self.itemconfirm.notify()
self.itemlock.release()