Skip to content

Commit eda2c79

Browse files
committed
Properly synchronize before incrementing current_job_index, otherwise
we may end up skipping some jobs and submitting some twice.
1 parent bc2bde5 commit eda2c79

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

tests/test_doc_examples.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Since they are actively tested against, they are certain to be
55
up-to-date and to work as intended.
66
"""
7-
7+
import threading
88
import time
99
import pathlib
1010
import typing
@@ -60,14 +60,16 @@ def __init__(self, total_jobs: int, max_active_jobs: int) -> None:
6060
self.current_job_index = 0
6161
self.total_jobs = total_jobs
6262
self.max_active_jobs = max_active_jobs
63+
self.lock = threading.RLock()
6364
if total_jobs < 1 or max_active_jobs < 1:
6465
raise ValueError("total_jobs and max_active_jobs must be > 0")
6566

6667
def submit_next(self) -> None:
6768
"""Submit the next job in the queue."""
68-
if self.current_job_index < self.total_jobs:
69-
self.jex.submit(self.jobs[self.current_job_index])
70-
self.current_job_index += 1
69+
with self.lock:
70+
if self.current_job_index < self.total_jobs:
71+
self.jex.submit(self.jobs[self.current_job_index])
72+
self.current_job_index += 1
7173

7274
def start(self) -> None:
7375
"""Begin submission of jobs."""

0 commit comments

Comments
 (0)