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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from queue import Queue

import random

class Checkpoint:

    def __init__(self, worker_count=1):
        self.queue = Queue()
        self.worker_count = worker_count
        self.active_travelers = []

    @property
    def free_worker_count(self):
        """Return the number of free workers"""
        return self.worker_count - len(self.active_travelers)

    def move_line(self):
        """Simulate the line moving up to empty checkpoint workers."""
        if not self.queue.empty() and self.free_worker_count:
            for i in range(0, self.free_worker_count):
                if self.queue.empty():
                    continue
                self.active_travelers.append(self.queue.get_nowait())

    def work(self):
        """Work on queue and active_travelers, returns completed_travelers."""
        completed_travelers = []

        # iterate over tmp_travelers so we can mutate active_travelers.
        # make copy of active_travelers called tmp_travelers.
        tmp_travelers = list(self.active_travelers)
        self.active_travelers = []

        for traveler in tmp_travelers:
            #if traveler.completed_state_transition:
            if random.choice([False, False, False, True]):
                # transition traveler to it's next state.
                #traveler.next_state()
                # append traveler to completed_traveler list.
                completed_travelers.append(traveler)
            else:
                self.active_travelers.append(traveler)

        # return completed_travelers so they move to into the queue for the next checkpoint.
        return completed_travelers




##############


import time

import random

from checkpoint import Checkpoint

traveler_enqueue_frequency = [
 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4, 5
]

ticket_checkpoint = Checkpoint(3)
item_checkpoint = Checkpoint(3)
body_checkpoint = Checkpoint(1)

tick = 0

report = """
tick: {}
ticket_queue_size: {}
ticket_active_travelers: {}
"""

while True:

    tick += 1

    for i in range(0, random.choice(traveler_enqueue_frequency)):
        ticket_checkpoint.queue.put("{}-{}".format(tick, i))
    print(
        report.format(
            tick,
            ticket_checkpoint.queue.qsize(),
            len(ticket_checkpoint.active_travelers),
        )
    )

    ticket_checkpoint.move_line()
    ticket_checkpoint.work()

    time.sleep(1)