oprunner 0.1.0.dev0__tar.gz → 0.1.0.dev1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: oprunner
3
- Version: 0.1.0.dev0
3
+ Version: 0.1.0.dev1
4
4
  Summary: Run image-processing ops in their own environments, with Appose.
5
5
  Author: scikit-ops developers
6
6
  License-Expression: BSD-2-Clause
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "oprunner"
7
- version = "0.1.0.dev0"
7
+ version = "0.1.0.dev1"
8
8
  description = "Run image-processing ops in their own environments, with Appose."
9
9
  license = "BSD-2-Clause"
10
10
  authors = [{name = "scikit-ops developers"}]
@@ -8,6 +8,7 @@ living in the op's declared environment.
8
8
 
9
9
  from __future__ import annotations
10
10
 
11
+ import atexit
11
12
  import contextlib
12
13
  import contextvars
13
14
  import functools
@@ -16,6 +17,8 @@ import itertools
16
17
  import json
17
18
  import re
18
19
  import sys
20
+ import threading
21
+ import weakref
19
22
  from collections.abc import Callable, Iterator
20
23
  from dataclasses import dataclass
21
24
  from pathlib import Path
@@ -150,6 +153,32 @@ def _default_envs_dir(root: Path) -> Path | None:
150
153
  return checkout if checkout.is_dir() else None
151
154
 
152
155
 
156
+ #: Every Runner not yet garbage-collected, so their workers can be closed at
157
+ #: interpreter exit.
158
+ _live_runners: weakref.WeakSet = weakref.WeakSet()
159
+
160
+
161
+ def _close_live_runners() -> None:
162
+ for runner in list(_live_runners):
163
+ with contextlib.suppress(Exception):
164
+ runner.close()
165
+
166
+
167
+ # A worker lives until its stdin closes, and Appose watches each one with
168
+ # three non-daemon threads (stdout, stderr, process monitor) that end only
169
+ # when it does. A runner left open -- a script that never calls close(), or
170
+ # a GUI's one shared runner -- therefore kept Python from exiting: shutdown
171
+ # waits on non-daemon threads, forever. atexit runs too late for this, after
172
+ # that wait; threading's own hook runs before it, and is what
173
+ # concurrent.futures uses for the same reason. It is private, so atexit is
174
+ # the fallback, which at least closes workers for anyone whose threads are
175
+ # already gone.
176
+ if hasattr(threading, "_register_atexit"):
177
+ threading._register_atexit(_close_live_runners)
178
+ else: # pragma: no cover
179
+ atexit.register(_close_live_runners)
180
+
181
+
153
182
  class Runner:
154
183
  """Builds environments, keeps workers warm, and dispatches op calls."""
155
184
 
@@ -169,6 +198,7 @@ class Runner:
169
198
  self._build_progress: list[Callable[[str, int, int], None]] = []
170
199
  self._build_output: list[Callable[[str], None]] = []
171
200
  self._build_error: list[Callable[[str], None]] = []
201
+ _live_runners.add(self)
172
202
 
173
203
  # -- build reporting -------------------------------------------------
174
204
 
File without changes
File without changes
File without changes