Commit f94c191
authored
Lazy napari checks for BatchRunner to improve import times (#5)
This pull request refactors how the code detects and handles the
presence of the `napari` library for threaded batch operations. Instead
of checking for `napari` at module import time, the code now tries to
import `napari` only when needed, improving flexibility and testability.
The test suite is also updated to simulate the absence of `napari` by
monkeypatching the import mechanism.
**Refactoring napari detection and threading logic:**
* Removed the module-level `HAS_NAPARI` flag and deferred importing
`napari` to runtime, allowing the code to try using napari threading if
available and fall back to standard threading otherwise
(`src/nbatch/_runner.py`)
[[1]](diffhunk://#diff-0dd9aa30afa1d11d42cc399298b79ac401045ee0c5261caafff03f7a7ee41597L26-L34)
[[2]](diffhunk://#diff-0dd9aa30afa1d11d42cc399298b79ac401045ee0c5261caafff03f7a7ee41597L238-R238).
* Updated the `cancel` method to check for a `quit` method on the worker
instead of relying on the `HAS_NAPARI` flag, making cancellation logic
more robust and decoupled from napari-specific checks
(`src/nbatch/_runner.py`).
* Moved the import of `create_worker` into the `_run_napari_threaded`
method so it only occurs when napari threading is actually used
(`src/nbatch/_runner.py`).
**Testing improvements:**
* Updated the threaded fallback test to simulate napari's absence by
monkeypatching Python's import mechanism, ensuring the fallback path is
tested regardless of napari's actual installation
(`tests/test_runner.py`)
[[1]](diffhunk://#diff-55824c57bc66ac238570c2acc0d4904ab52df3131aae66c0ecefc2f08a4e8806L492-R502)
[[2]](diffhunk://#diff-55824c57bc66ac238570c2acc0d4904ab52df3131aae66c0ecefc2f08a4e8806L509-R516).1 parent d3a2ddb commit f94c191
2 files changed
+28
-24
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | 26 | | |
36 | 27 | | |
37 | 28 | | |
| |||
163 | 154 | | |
164 | 155 | | |
165 | 156 | | |
166 | | - | |
| 157 | + | |
| 158 | + | |
167 | 159 | | |
168 | | - | |
169 | | - | |
| 160 | + | |
| 161 | + | |
170 | 162 | | |
171 | 163 | | |
172 | 164 | | |
| |||
235 | 227 | | |
236 | 228 | | |
237 | 229 | | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
246 | 242 | | |
247 | 243 | | |
248 | 244 | | |
| |||
285 | 281 | | |
286 | 282 | | |
287 | 283 | | |
| 284 | + | |
288 | 285 | | |
289 | 286 | | |
290 | 287 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
489 | 489 | | |
490 | 490 | | |
491 | 491 | | |
492 | | - | |
493 | | - | |
| 492 | + | |
| 493 | + | |
494 | 494 | | |
495 | | - | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
496 | 503 | | |
497 | 504 | | |
498 | 505 | | |
| |||
506 | 513 | | |
507 | 514 | | |
508 | 515 | | |
509 | | - | |
| 516 | + | |
510 | 517 | | |
511 | 518 | | |
512 | 519 | | |
| |||
0 commit comments