MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / simulate_pipeline

Function simulate_pipeline

example_code/item_073.py:159–177  ·  view source on GitHub ↗
(grid, in_queue, out_queue)

Source from the content-addressed store, hash-verified

157 return count
158
159def simulate_pipeline(grid, in_queue, out_queue):
160 for y in range(grid.height):
161 for x in range(grid.width):
162 state = grid.get(y, x)
163 neighbors = count_neighbors(y, x, grid.get)
164 in_queue.put((y, x, state, neighbors)) # Fan-out
165
166 in_queue.join()
167 item_count = out_queue.qsize()
168
169 next_grid = Grid(grid.height, grid.width)
170 for _ in range(item_count):
171 item = out_queue.get() # Fan-in
172 y, x, next_state = item
173 if isinstance(next_state, Exception):
174 raise SimulationError(y, x) from next_state
175 next_grid.set(y, x, next_state)
176
177 return next_grid
178
179
180print("Example 4")

Callers 1

item_073.pyFile · 0.85

Calls 6

setMethod · 0.95
SimulationErrorClass · 0.85
putMethod · 0.80
count_neighborsFunction · 0.70
GridClass · 0.70
getMethod · 0.45

Tested by

no test coverage detected