-
-
Notifications
You must be signed in to change notification settings - Fork 459
WIP, BUG: futures with dir switching #2069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| import glob | ||
| import itertools | ||
| import os | ||
| import shutil | ||
| import random | ||
| import socket | ||
| import sqlite3 | ||
|
|
@@ -232,6 +233,8 @@ def __init__( | |
| no_disk: bool = False, | ||
| warn: TWarnFn | None = None, | ||
| debug: TDebugCtl | None = None, | ||
| *, | ||
| orig_dir = None, | ||
| ) -> None: | ||
| """Create a :class:`CoverageData` object to hold coverage-measured data. | ||
|
|
||
|
|
@@ -271,6 +274,7 @@ def __init__( | |
| self._current_context: str | None = None | ||
| self._current_context_id: int | None = None | ||
| self._query_context_ids: list[int] | None = None | ||
| self.orig_dir = orig_dir | ||
|
|
||
| __repr__ = auto_repr | ||
|
|
||
|
|
@@ -898,6 +902,11 @@ def read(self) -> None: | |
| def write(self) -> None: | ||
| """Ensure the data is written to the data file.""" | ||
| self._debug_dataio("Writing (no-op) data file", self._filename) | ||
| if self.orig_dir is not None: | ||
| for fname in os.listdir(): | ||
| if fname.startswith(".coverage."): | ||
| if not os.path.exists(os.path.join(self.orig_dir, fname)): | ||
| shutil.copy(fname, self.orig_dir) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line here does the trick to restore coverage of the missing import os
import tempfile
from concurrent.futures import ProcessPoolExecutor
def add(a, b):
return a + b
def probe_dispatcher():
orig_dir = os.getcwd()
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
dispatcher()
os.chdir(orig_dir)
def dispatcher():
futures = []
with ProcessPoolExecutor() as executor:
futures.append(executor.submit(add, 2, 2))
for future in futures:
future.result()
if __name__ == "__main__":
probe_dispatcher()
whereas suppressing that
While I can reproduce this lack of coverage with the new regression test I added here, I can't make that test pass on this feature branch at the moment... am I missing something about the meta testing/coverage testing harness? |
||
|
|
||
| def _start_using(self) -> None: | ||
| """Call this before using the database at all.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| import re | ||
| import sys | ||
| import threading | ||
| import concurrent.futures | ||
| import time | ||
|
|
||
| from types import ModuleType | ||
|
|
@@ -412,6 +413,32 @@ def work(x): | |
| return sum_range((x+1)*100) | ||
| """ | ||
|
|
||
| MULTI_CODE_DIR_CHANGE = """ | ||
| import os | ||
| import tempfile | ||
| from concurrent.futures import ProcessPoolExecutor | ||
|
|
||
| def add(a, b): | ||
| return a + b | ||
|
|
||
| def probe_dispatcher(): | ||
| orig_dir = os.getcwd() | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| os.chdir(temp_dir) | ||
| dispatcher() | ||
| os.chdir(orig_dir) | ||
|
|
||
| def dispatcher(): | ||
| futures = [] | ||
| with ProcessPoolExecutor({NPROCS}) as executor: | ||
| futures.append(executor.submit(add, 2, 2)) | ||
| for future in futures: | ||
| future.result() | ||
|
|
||
| if __name__ == "__main__": | ||
| probe_dispatcher() | ||
| """ | ||
|
|
||
| MULTI_CODE = """ | ||
| # Above this will be a definition of work(). | ||
| import multiprocessing | ||
|
|
@@ -522,6 +549,20 @@ def test_multiprocessing_simple(self, start_method: str) -> None: | |
| start_method=start_method, | ||
| ) | ||
|
|
||
| def test_gh_2065(self, start_method: str) -> None: | ||
| nprocs = 1 | ||
| upto = 30 | ||
| code = (MULTI_CODE_DIR_CHANGE).format(NPROCS=nprocs, UPTO=upto) | ||
| total = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| expected_out = f"" | ||
| self.try_multiprocessing_code( | ||
| code, | ||
| expected_out, | ||
| concurrent.futures, | ||
| nprocs, | ||
| start_method=start_method, | ||
| ) | ||
|
|
||
| def test_multiprocessing_append(self, start_method: str) -> None: | ||
| nprocs = 3 | ||
| upto = 30 | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels kind of bad... even more configuration/hacking via the
multiprocessingmonkeypatching, but it does seem to work as part of storing the original working directory for the originally described issue, so that.coverage.files may be copied back there.