Skip to content

Commit 9ab9b67

Browse files
committed
Warn if numpy installed but not imported via init
See apposed/appose#23.
1 parent b6bab95 commit 9ab9b67

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/appose/python_worker.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,39 @@ def main() -> None:
271271
except BaseException as e:
272272
print(f"[WARNING] Init script failed: {e}", file=sys.stderr)
273273

274+
# On Windows, we must import numpy here on the main thread before opening stdin.
275+
# Otherwise, the import will hang, even if run as part of a task with queue="main".
276+
# See: https://github.com/numpy/numpy/issues/24290.
277+
# The best way to do that is by creating the Python service like:
278+
#
279+
# env.python().init("import numpy")
280+
#
281+
# or similar `init` script invocation.
282+
#
283+
# We check here whether the hanging conditions might be met: NumPy installed,
284+
# but not imported yet. And if so, issue a stern warning, as a kindness.
285+
try:
286+
from importlib.metadata import distributions
287+
288+
numpy_installed = any(
289+
dist.metadata["Name"] == "numpy" for dist in distributions()
290+
)
291+
if numpy_installed and "numpy" not in globals():
292+
print(
293+
"[WARNING] This environment includes numpy, but numpy was not imported via a service init script.",
294+
file=sys.stderr,
295+
)
296+
print(
297+
"[WARNING] If you attempt to `import numpy` in a task on Windows, the task will hang!",
298+
file=sys.stderr,
299+
)
300+
print(
301+
"[WARNING] See https://github.com/apposed/appose/issues/23 for details.",
302+
file=sys.stderr,
303+
)
304+
except Exception:
305+
pass
306+
274307
worker.run()
275308

276309

0 commit comments

Comments
 (0)