bslatkin / effectivepython

Effective Python: Second Edition — Source Code and Errata for the Book
https://effectivepython.com
2.2k stars 710 forks source link

Item 58, page 260 of 2nd Edition: columns display shows dead grids #70

Open davewsmith opened 4 years ago

davewsmith commented 4 years ago

The text reads "The results are as before," but the 1st and 3rd generations show a dead grid.

bslatkin commented 4 years ago

Hi Dave! Good to hear from you. Thank you for the report. Sorry for my delay. This is definitely a bug in the code that was introduced by editing and reordering the code example. The problem here is that the sentinel object used for closing isn't getting removed from the out_queue in this example:

def game_logic(state, neighbors):
    ...
    raise OSError('Problem with I/O in game_logic')
    ...

simulate_pipeline(Grid(1, 1), in_queue, out_queue)

Which outputs this:

Traceback ...
OSError: Problem with I/O in game_logic

The above exception was the direct cause of the following exception:

Traceback ...
SimulationError: (0, 0)

To fix this, I need to change the code sample that follows so it cleans things up in the out_queue appropriately:

I can drive this multi-threaded pipeline for repeated generations by calling simulate_pipeline in a loop.

# HIDE
# Clear the sentinel object from the out queue
for _ in out_queue:
    pass

...