Skip to content

Commit 88265be

Browse files
committed
Add explanation for error when defining task types from an interactive shell on Windows and macOS.
1 parent 82b7eec commit 88265be

File tree

2 files changed

+103
-58
lines changed

2 files changed

+103
-58
lines changed

docs/cookbook.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,9 +948,10 @@ results = lab.run_tasks(runs)
948948
### Why do I see the following error: `An attempt has been made to start a new process before the current process has finished`?
949949

950950
When running labtech in a Python script on Windows, macOS, or any
951-
Python environment using the `spawn` multiprocessing start method, you
952-
will see the following error if you do not guard your experiment and
953-
lab creation and other non-definition code with `__name__ ==
951+
Python environment using the
952+
[`spawn` multiprocessing start method](https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods),
953+
you will see the following error if you do not guard your experiment
954+
and lab creation and other non-definition code with `__name__ ==
954955
'__main__'`:
955956

956957
```
@@ -1000,3 +1001,24 @@ if __name__ == '__main__':
10001001
```
10011002

10021003
For details, see [Safe importing of main module](https://docs.python.org/3/library/multiprocessing.html#multiprocessing-safe-main-import).
1004+
1005+
1006+
### Why do I see the following error: `AttributeError: Can't get attribute 'YOUR_TASK_CLASS' on <module '__main__' (built-in)>`?
1007+
1008+
You will see this error (as part of a very long stack trace) when
1009+
defining and running labtech tasks from an interactive Python shell on
1010+
Windows or macOS (or more specifically, when
1011+
[Python's multiprocessing start method](https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods)
1012+
has been set to `spawn` or `forkserver`).
1013+
1014+
The solution to this error is to define all of your labtech `Task`
1015+
types in a separate `.py` Python module file which you can import into
1016+
your interactive shell session (e.g. `from my_module import MyTask`).
1017+
1018+
The reason for this error is that `spawn` and `forkserver` start
1019+
methods will not copy the current state of your `__main__` module
1020+
(which contains the variables you declare interactively in the Python
1021+
shell, including task definitions) into labtech's task subprocesses.
1022+
This error does not occur for the `fork` start method (the current
1023+
default on Linux) because forked subprocesses *do* receive the current
1024+
state of all modules (including `__main__`) from the parent process.

0 commit comments

Comments
 (0)