File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed
Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change 1+ # Race condition in orignal procs.py
2+
3+ Thanks to reader Michael Albert who noticed the code I published during the Early Release had a race condition in ` proc.py ` .
4+
5+ If you are curious,
6+ [ this diff] ( https://github.com/fluentpython/example-code-2e/commit/2c1230579db99738a5e5e6802063bda585f6476d )
7+ shows the bug and how I fixed it—but note that I later refactored
8+ the example to delegate parts of ` main ` to the ` start_jobs ` and ` report ` functions.
9+
10+ The problem was that I ended the ` while ` loop that retrieved the results when the ` jobs ` queue was empty.
11+ However, it was possible that the queue was empty but there were still processes working.
12+ If that happened, one or more results would not be reported.
13+ I did not notice the problem when I tested my original code,
14+ but Albert showed that adding a ` sleep(1) ` call before the ` if jobs.empty() ` line made the bug occur frequently.
15+ I adopted one of his solutions: have the ` worker ` function send back a ` PrimeResult ` with ` n = 0 ` as a sentinel,
16+ to let the main loop know that the process had completed, ending the loop when all processes were done.
You can’t perform that action at this time.
0 commit comments