Skip to content

Commit 86edd34

Browse files
committed
Document sync_main_only flag and use cases.
1 parent 4422cac commit 86edd34

File tree

3 files changed

+72
-24
lines changed

3 files changed

+72
-24
lines changed

docs/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ solution. (These requirements are explored
9191
workers are limited to one way calls from the main thread to methods
9292
exposed by workers.
9393

94-
If `sync_main_only = True`, the following caveats apply:
94+
If `sync_main_only = true`, the following caveats apply:
9595

9696
* It is not possible to manipulate the DOM or do anything meaningful on the
9797
main thread **from a worker**. This is because Atomics cannot guarantee

docs/user-guide/configuration.md

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ ensure the packages [arrr](https://arrr.readthedocs.io/en/latest/) and
3535
[numberwang](https://numberwang.readthedocs.io/en/latest/) are installed from
3636
PyPI (the [Python Packaging Index](https://pypi.org/)):
3737

38-
```TOML title="Configuration via TOML"
38+
```TOML title="Configuration via TOML."
3939
packages = ["arrr", "numberwang" ]
4040
```
4141

42-
```JSON title="Configuration via JSON"
42+
```JSON title="Configuration via JSON."
4343
{
4444
"packages": ["arrr", "numberwang"]
4545
}
@@ -56,15 +56,15 @@ reference it from the tag used to specify the Python code:
5656

5757
If you use JSON, you can make it the value of the `config` attribute:
5858

59-
```HTML title="JSON as the value of the config attribute"
59+
```HTML title="JSON as the value of the config attribute."
6060
<script type="mpy" src="main.py" config='{"packages":["arrr", "numberwang"]}'></script>
6161
```
6262

6363
For historical and convenience reasons we still support the inline
6464
specification of configuration information via a _single_ `<py-config>` or
6565
`<mpy-config>` tag in your HTML document:
6666

67-
```HTML title="Inline configuration via the &lt;py-config&gt; tag"
67+
```HTML title="Inline configuration via the &lt;py-config&gt; tag."
6868
<py-config>
6969
{
7070
"packages": ["arrr", "numberwang" ]
@@ -79,10 +79,10 @@ specification of configuration information via a _single_ `<py-config>` or
7979

8080
## Options
8181

82-
There are four core options ([`interpreter`](#interpreter), [`files`](#files),
83-
[`packages`](#packages), and
84-
[`js_modules`](#javascript-modules)) and an experimental flag
85-
([experimental_create_proxy](#experimental_create_proxy)) that can be used in
82+
There are five core options ([`interpreter`](#interpreter), [`files`](#files),
83+
[`packages`](#packages), [`js_modules`](#javascript-modules) and
84+
[`sync_main_only`](#sync_main_only)) and an experimental flag
85+
([`experimental_create_proxy`](#experimental_create_proxy)) that can be used in
8686
the configuration of PyScript. The user is also free to define
8787
arbitrary additional configuration options that plugins or an app may require
8888
for their own reasons.
@@ -100,11 +100,11 @@ a custom version of the interpreter.
100100

101101
The following two examples are equivalent:
102102

103-
```TOML title="Specify the interpreter version in TOML"
103+
```TOML title="Specify the interpreter version in TOML."
104104
interpreter = "0.23.4"
105105
```
106106

107-
```JSON title="Specify the interpreter version in JSON"
107+
```JSON title="Specify the interpreter version in JSON."
108108
{
109109
"interpreter": "0.23.4"
110110
}
@@ -113,7 +113,7 @@ interpreter = "0.23.4"
113113
The following JSON fragment uses a fully qualified URL to point to the same
114114
version of Pyodide as specified in the previous examples:
115115

116-
```JSON title="Specify the interpreter via a fully qualified URL"
116+
```JSON title="Specify the interpreter via a fully qualified URL."
117117
{
118118
"interpreter": "https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.mjs"
119119
}
@@ -127,7 +127,7 @@ destination filesystem path.
127127

128128
The following JSON and TOML are equivalent:
129129

130-
```json title="Fetch files onto the filesystem with JSON"
130+
```json title="Fetch files onto the filesystem with JSON."
131131
{
132132
"files": {
133133
"https://example.com/data.csv": "./data.csv",
@@ -136,7 +136,7 @@ The following JSON and TOML are equivalent:
136136
}
137137
```
138138

139-
```toml title="Fetch files onto the filesystem with TOML"
139+
```toml title="Fetch files onto the filesystem with TOML."
140140
[files]
141141
"https://example.com/data.csv" = "./data.csv"
142142
"/code.py" = "./subdir/code.py"
@@ -147,7 +147,7 @@ URL becomes the destination filename, in the root of the filesystem, to which
147147
the content is copied. As a result, the `data.csv` entry from the previous
148148
examples could be equivalently re-written as:
149149

150-
```json title="JSON implied filename in the root directory"
150+
```json title="JSON implied filename in the root directory."
151151
{
152152
"files": {
153153
"https://example.com/data.csv": "",
@@ -156,7 +156,7 @@ examples could be equivalently re-written as:
156156
}
157157
```
158158

159-
```toml title="TOML implied filename in the root directory"
159+
```toml title="TOML implied filename in the root directory."
160160
[files]
161161
"https://example.com/data.csv" = ""
162162
... etc ...
@@ -202,7 +202,7 @@ their name is replaced with their associated value.
202202

203203
The following JSON and TOML are equivalent:
204204

205-
```json title="Using the template language in JSON"
205+
```json title="Using the template language in JSON."
206206
{
207207
"files": {
208208
"{DOMAIN}": "https://my-server.com",
@@ -218,7 +218,7 @@ The following JSON and TOML are equivalent:
218218
}
219219
```
220220

221-
```toml title="Using the template language in TOML"
221+
```toml title="Using the template language in TOML."
222222
[files]
223223
"{DOMAIN}" = "https://my-server.com"
224224
"{PATH}" = "a/path"
@@ -287,11 +287,11 @@ to be installed onto the Python path.
287287

288288
The following two examples are equivalent:
289289

290-
```TOML title="A packages list in TOML"
290+
```TOML title="A packages list in TOML."
291291
packages = ["arrr", "numberwang", "snowballstemmer>=2.2.0" ]
292292
```
293293

294-
```JSON title="A packages list in JSON"
294+
```JSON title="A packages list in JSON."
295295
{
296296
"packages": ["arrr", "numberwang", "snowballstemmer>=2.2.0" ]
297297
}
@@ -338,13 +338,13 @@ To specify such modules, simply provide a list of source/module name pairs.
338338
For example, to use the excellent [Leaflet](https://leafletjs.com/) JavaScript
339339
module for creating interactive maps you'd add the following lines:
340340

341-
```TOML title="JavaScript main thread modules defined in TOML"
341+
```TOML title="JavaScript main thread modules defined in TOML."
342342
[js_modules.main]
343343
"https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet-src.esm.js" = "leaflet"
344344
"https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css" = "leaflet" # CSS
345345
```
346346

347-
```JSON title="JavaScript main thread modules defined in JSON"
347+
```JSON title="JavaScript main thread modules defined in JSON."
348348
{
349349
"js_modules": {
350350
"main": {
@@ -386,12 +386,12 @@ Some JavaScript modules (such as
386386
access to the DOM and, for efficiency reasons, can be included in the worker
387387
context:
388388

389-
```TOML title="A JavaScript worker module defined in TOML"
389+
```TOML title="A JavaScript worker module defined in TOML."
390390
[js_modules.worker]
391391
"https://cdn.jsdelivr.net/npm/html-escaper" = "html_escaper"
392392
```
393393

394-
```JSON title="A JavaScript worker module defined in JSON"
394+
```JSON title="A JavaScript worker module defined in JSON."
395395
{
396396
"js_modules": {
397397
"worker": {
@@ -404,6 +404,40 @@ context:
404404
However, `from pyscript.js_modules import html_escaper` would then only work
405405
within the context of Python code **running on a worker**.
406406

407+
### sync_main_only
408+
409+
Sometimes you just want to start an expensive computation on a web worker
410+
without the need for the worker to interact with the main thread. You're simply
411+
awaiting the result of a method exposed from a worker.
412+
413+
This has the advantage of not requiring the use of `SharedArrayBuffer` and
414+
[associated CORS related header configuration](../workers/#http-headers).
415+
416+
If the `sync_main_only` flag is set, then **interactions between the main thread
417+
and workers are limited to one way calls from the main thread to methods
418+
exposed by the workers**.
419+
420+
```TOML title="Setting the sync_main_only flag in TOML."
421+
sync_main_only = true
422+
```
423+
424+
```JSON title="Setting the sync_main_only flag in JSON."
425+
{
426+
"sync_main_only": true
427+
}
428+
```
429+
430+
If `sync_main_only` is set, the following caveats apply:
431+
432+
* It is not possible to manipulate the DOM or do anything meaningful on the
433+
main thread **from a worker**. This is because Atomics cannot guarantee
434+
sync-like locks between a worker and the main thread.
435+
* Only a worker's `pyscript.sync` methods are exposed, and **they can only be
436+
awaited from the main thread**.
437+
* The worker can only `await` main thread references one after the other, so
438+
developer experience is degraded when one needs to interact with the
439+
main thread.
440+
407441
### experimental_create_proxy
408442

409443
Knowing when to use the `pyscript.ffi.create_proxy` method when using Pyodide

docs/user-guide/workers.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ You don't need to know about Atomics to use web workers, but the underlying
1111
[coincident library](http://localhost:8000/user-guide/architecture/#coincident)
1212
uses it under the hood.
1313

14+
!!! info
15+
16+
Sometimes you only need to `await` in the main thread the result of a call
17+
to a method exposed in a worker.
18+
19+
In such a limited case, and on the understanding that **code in the worker
20+
will not be able to reach back into the main thread**, you should
21+
use the [`sync_main_only` flag](../configuration/#sync_main_only) in your
22+
configuration.
23+
24+
While this eliminates the need for the Atomics related header configuration
25+
(see below), the only possible use case is to **return a serialisable
26+
result from the method called on the worker**.
27+
1428
## HTTP headers
1529

1630
For Atomics to work **you must ensure your web server enables the following

0 commit comments

Comments
 (0)