def build_timeline(): def cvt(events, waiters): for event in events.get(): event.trigger() for waiter in event.waiters: waiters.put(waiter) r = ContainerStack() r.add_layer(container=BatchHeap(lambda x, y: x.now < y.now), get=BatchHeap.pop, put=BatchHeap.push, broker=cvt) r.add_layer(container=BatchHeap(lambda x, y: x.priority > y.priority), get=BatchHeap.pop, put=BatchHeap.push) r.add_layer(container=deque(), get=deque.popleft, put=deque.extend) return r
def build_timeline(): def cvt(events, waiters): for event in events.get(): event.trigger() for waiter in event.waiters: waiters.put(waiter) r = ContainerStack() r.add_layer(container=BatchHeap(lambda x,y: x.now < y.now), get=BatchHeap.pop, put=BatchHeap.push, broker=cvt) r.add_layer(container=BatchHeap(lambda x,y: x.priority > y.priority), get=BatchHeap.pop, put=BatchHeap.push) r.add_layer(container=deque(), get=deque.popleft, put=deque.extend) return r
while timeline: f() def step_thread(): print timeline.get() def step_same_priorty(): print timeline.get(depth=1) def step_same_now(): print timeline.get(depth=2) by_now = lambda x,y: x.now < y.now by_priority = lambda x,y: x.priority > y.priority timeline = ContainerStack() timeline.add_layer(container=BatchHeap(by_now), get=BatchHeap.pop, put=BatchHeap.push, broker=events2waiters ) timeline.add_layer(container=BatchHeap(by_priority), get=BatchHeap.pop, put=BatchHeap.push ) timeline.add_layer(container=deque(), get=deque.popleft, put=deque.extend ) thread_f = Thread('f', 0)
def step_thread(): print timeline.get() def step_same_priorty(): print timeline.get(depth=1) def step_same_now(): print timeline.get(depth=2) by_now = lambda x, y: x.now < y.now by_priority = lambda x, y: x.priority > y.priority timeline = ContainerStack() timeline.add_layer(container=BatchHeap(by_now), get=BatchHeap.pop, put=BatchHeap.push, broker=events2waiters) timeline.add_layer(container=BatchHeap(by_priority), get=BatchHeap.pop, put=BatchHeap.push) timeline.add_layer(container=deque(), get=deque.popleft, put=deque.extend) thread_f = Thread('f', 0) thread_g = Thread('g', 1) thread_h = Thread('h', 1) timeline.put(Event('Initial', 0, [thread_f])) timeline.put(Event('Initial', 0, [thread_g])) timeline.put(Event('Initial', 0, [thread_h]))