You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/QUICK_START.md
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -107,6 +107,24 @@ with install_package_as("PyYAML"):
107
107
108
108
Common mappings (`cv2` → `opencv-python`, `PIL` → `Pillow`, etc.) are built in.
109
109
110
+
### Worker-only imports
111
+
112
+
Skip installing packages locally — the worker installs them on demand:
113
+
114
+
```python
115
+
from pyfuse import worker_only_import
116
+
117
+
with worker_only_import():
118
+
import requests
119
+
120
+
with worker_only_import("opencv-python-headless"):
121
+
import cv2
122
+
```
123
+
124
+
The local `requests` and `cv2` resolve to lightweight stubs. They're fine to reference inside a `@trace` function (the worker re-imports them for real), but raise `WorkerOnlyError` if used directly on the client.
125
+
126
+
Only the names imported literally inside the `with` block are stubbed — real installed packages and their transitive imports are unaffected.
|`package`|`"opencv-python"` (from `install_package_as`, or `None`) |
445
+
|`package`|`"opencv-python"` (from `install_package_as` / `worker_only_import`, or `None`) |
446
+
|`worker_only`|`True` if the import was inside a `worker_only_import` block (omitted when `False`) |
446
447
447
448
Multi-name imports are split into individual objects for per-function tracking.
448
449
@@ -489,6 +490,24 @@ with install_package_as("opencv-python"):
489
490
490
491
The worker sees the `package` field on the import and knows to `pip install opencv-python` instead of `pip install cv2`.
491
492
493
+
### `worker_only_import` context manager
494
+
495
+
Lets clients reference a package without installing it locally. On the client, imports inside the block resolve to lightweight stub modules (`_WorkerOnlyStub`) that raise `WorkerOnlyError` if used outside a worker context. On the worker, the package is installed via pip and imported normally.
496
+
497
+
```python
498
+
with worker_only_import(): # import name == pip name
499
+
import requests
500
+
501
+
with worker_only_import("opencv-python-headless"): # explicit pip name
502
+
import cv2
503
+
```
504
+
505
+
Implementation:
506
+
- A meta-path finder is appended to `sys.meta_path` for the duration of the block (real installed packages still win because the finder runs last).
507
+
- The finder's whitelist is populated by parsing the caller's `with` block source via AST — only names the user literally writes inside the block are eligible to be stubbed. This prevents transitive missing imports from real installed packages from being silently swallowed.
508
+
- The AST analyzer marks every `ImportInfo` in the block with `worker_only=True` and records any explicit pip name as `package`.
509
+
- The worker treats worker-only imports identically to regular third-party imports: `extract_third_party_modules` collects them, `_collect_package_hints` honors the pip name, and `pip install` runs before reconstruction.
510
+
492
511
## Heartbeat and stall detection
493
512
494
513
Workers send periodic heartbeats (every 1 second) while executing a task via an `asyncio.Task`. Clients use heartbeat data for stall detection.
0 commit comments