Skip to content

Commit ad42cc0

Browse files
committed
Updated worker docs.
1 parent cad8fe6 commit ad42cc0

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

docs/api.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,59 @@ PyWorker("worker.py", type="micropython")
805805
<div id="output"></div> <!-- The display target -->
806806
```
807807
808+
### `pyscript.workers`
809+
810+
The `pyscript.workers` reference allows Python code in the main thread to
811+
easily access named workers (and their exported functionality).
812+
813+
For example, the following Pyodide code may be running on a named worker
814+
(see the `name` attribute of the `script` tag):
815+
816+
```html
817+
<script type="py" worker name="py-version">
818+
import sys
819+
820+
def version():
821+
return sys.version
822+
823+
# define what to export to main consumers
824+
__export__ = ["version"]
825+
</script>
826+
```
827+
828+
While over on the main thread, this fragment of MicroPython will be able to
829+
access the worker's `version` function via the `workers` reference:
830+
831+
```html
832+
<script type="mpy" async>
833+
from pyscript import workers
834+
835+
pyworker = await workers["py-version"]
836+
837+
# print the pyodide version
838+
print(await pyworker.version())
839+
</script>
840+
```
841+
842+
Importantly, the `workers` reference will **NOT** provide a list of
843+
known workers, but will only `await` for a reference to a named worker
844+
(resolving when the worker is ready). This is because the timing of worker
845+
startup is not deterministic.
846+
847+
Should you wish to await for all workers on the page at load time, it's
848+
possible to loop over matching elements in the document like this:
849+
850+
```html
851+
<script type="mpy" async>
852+
from pyscript import document, workers
853+
854+
for el in document.querySelectorAll("[type='py'][worker][name]"):
855+
await workers[el.getAttribute('name')]
856+
857+
# ... rest of the code
858+
</script>
859+
```
860+
808861
## Worker only features
809862

810863
### `pyscript.js_import`

docs/user-guide/workers.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ attribute flag:
6969
<script type="py" src="./my-worker-code.py" worker></script>
7070
```
7171

72+
You may also want to add a `name` attribute to the tag, so you can use
73+
`pyscript.workers` in the main thread to retrieve a reference to the worker:
74+
75+
```html
76+
<script type="py" src="./my-worker-code.py" worker name="my-worker"></script>
77+
```
78+
79+
```python
80+
from pyscript import workers
81+
82+
my_worker = await workers["my-worker"]
83+
```
84+
7285
Alternatively, to launch a worker from within Python running on the main thread
7386
use the [pyscript.PyWorker](../../api/#pyscriptpyworker) class and you must
7487
reference both the target Python script and interpreter type:
@@ -77,7 +90,7 @@ reference both the target Python script and interpreter type:
7790
from pyscript import PyWorker
7891

7992
# The type MUST be given and can be either `micropython` or `pyodide`
80-
PyWorker("my-worker-code.py", type="micropython")
93+
my_worker = PyWorker("my-worker-code.py", type="micropython")
8194
```
8295

8396
## Worker interactions
@@ -121,6 +134,34 @@ greeting = sync.hello("PyScript")
121134
window.console.log(greeting)
122135
```
123136

137+
Alternatively, for the main thread to call functions in a worker, specify the
138+
functions in a `__export__` list:
139+
140+
```python title="Python code on the worker."
141+
import sys
142+
143+
def version():
144+
return sys.version
145+
146+
# Define what to export to the main thread.
147+
__export__ = ["version", ]
148+
```
149+
150+
Then ensure you have a reference to the worker in the main thread (for
151+
instance, by using the `pyscript.workers`):
152+
153+
```html title="Creating a named worker in the web page."
154+
<script type="py" src="./my-worker-code.py" worker name="my-worker"></script>
155+
```
156+
157+
```python title="Referencing and using the worker from the main thread."
158+
from pyscript import workers
159+
160+
my_worker = await workers["my-worker"]
161+
162+
print(await my_worker.version())
163+
```
164+
124165
The values passed between the main thread and the worker **must be
125166
serializable**. Try the example given above via
126167
[this project on PyScript.com](https://pyscript.com/@ntoll/tiny-silence/latest).

0 commit comments

Comments
 (0)