Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4fad21e

Browse filesBrowse files
committed
added README.md to 19-concurrency/primes/
1 parent d59215d commit 4fad21e
Copy full SHA for 4fad21e

File tree

Expand file treeCollapse file tree

1 file changed

+16
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-0
lines changed

‎19-concurrency/primes/README.md

Copy file name to clipboard
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.