-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_consumer.py
50 lines (36 loc) · 1.06 KB
/
producer_consumer.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
class Job(object):
def __init__(self, description):
self.description = description
self.previous = None
self.next = None
def execute(self):
pass
class JobPool(object):
def __init__(self):
self.jobs = []
self.head = None
self.tail = None
def add_job(self, job):
# check if there is a head
if self.head is None:
self.head = job
self.tail = job
else:
job.previous = self.tail
self.tail.next = job
self.tail = self.tail.next
def get_job(self):
# get head (job) and we will return this once we reassign head.
if self.head is None:
return None
job = self.head
# reassign head
if self.head == self.tail:
self.head = None
self.tail = None
else:
self.head = self.head.next
# new head needs previous to point to None, next is unchanged
self.head.previous = None
job.next = None
return job