Skip to content

Commit d5dc4f1

Browse files
committed
Edits.
1 parent 24f98a5 commit d5dc4f1

File tree

3 files changed

+82
-71
lines changed

3 files changed

+82
-71
lines changed

docs/faq.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,30 @@ This is the first and most common error users may encounter with PyScript:
6363

6464
#### When
6565

66-
This error happens when **the server delivering your PyScript application is
67-
incorrectly configured** or no *service-worker* attribute fallback
68-
has been provided, hence failing to enable access to `window` and/or `document`
69-
on the *main* thread.
70-
71-
This happens when:
72-
73-
* There is a `worker` attribute in the *py* or *mpy* script element, its code
74-
accesses/imports either `window` or `document` but there's no way to use these
75-
due platform limitations on synchronous *Atomics* operations.
76-
* There is a `<script type="py-editor">` that always uses a *worker* behind the
77-
scenes and no fallback has been provided via `service-worker` attribute.
78-
* There is an explicit `PyWorker` or `MPWorker` bootstrapping somewhere in your
79-
code and no `service_worker` fallback has been provided.
66+
This happens when you're unable to access objects in the main thread (`window`
67+
and `document`) from code running in a web worker.
68+
69+
This error happens because **the server delivering your PyScript application is
70+
incorrectly configured** or **a `service-worker` attribute has not been used in
71+
your `script` element**.
72+
73+
Specifically, one of the following three problem situations applies to your
74+
code:
75+
76+
* Because of the way your web server is configured, the browser limits the use
77+
of a technology called "Atomics" (you don't need to know how it works, just
78+
that it may be limited by the browser). If there is a `worker` attribute in
79+
your `script` element, and your Python code uses the `window` or `document`
80+
objects (that actually exist on the main thread), then the browser limitation
81+
on Atomics will cause the failure, unless you reconfigure your server.
82+
* There is a `<script type="py-editor">` (that must always use a worker behind
83+
the scenes) and no fallback has been provided via a `service-worker`
84+
attribute on that element.
85+
* There is an explicit `PyWorker` or `MPWorker` instance **bootstrapping
86+
somewhere in your code** and no `service_worker` fallback has been provided.
8087

8188
All these cases have been documented with code examples and possible solutions
82-
in our [Web Workers](https://docs.pyscript.net/latest/user-guide/workers/) section.
89+
in our section on [web workers](../user-guide/workers/).
8390

8491
#### Why
8592

@@ -98,7 +105,7 @@ CPU. It idles until the referenced index of the shared buffer changes,
98105
effectively never blocking the main thread while still pausing its own
99106
execution until the buffer's index is changed.
100107

101-
As overwhelming or complicated as this might sounds, these two fundamental
108+
As overwhelming or complicated as this might sound, these two fundamental
102109
primitives make main ↔ worker interoperability an absolute wonder in term of
103110
developer experience. Therefore, we encourage folks to prefer using workers
104111
over running Python in the main thread. This is especially so when using
@@ -107,14 +114,10 @@ requirements. Using workers ensures the main thread (and thus, the user
107114
interface) remains unblocked.
108115

109116
Unfortunately, we can patch, polyfill, or workaround, these primitives but
110-
we cannot change their intrinsic standard nature and limitations.
111-
112-
We do, however, offer various solutions that circumvent security concerns
113-
around special headers or enable native functionality with relative ease.
114-
115-
Please read our
116-
[Web Workers](https://docs.pyscript.net/latest/user-guide/workers/)
117-
dedicated section to know more about possible solutions.
117+
we cannot change their intrinsic nature and limitations defined by web
118+
standards. However, there are various solutions for working around such
119+
limitations. Please read our [web workers](..//user-guide/workers/)
120+
section to learn more.
118121

119122
### Borrowed proxy
120123

docs/user-guide/first-steps.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,18 @@ attributes:
8080
* `worker` - a flag to indicate your Python code is to be run on a
8181
[web worker](workers.md) instead of the "main thread" that looks after the user
8282
interface.
83-
* `target` - The id or selector of the element where calls to
83+
* `target` - the id or selector of the element where calls to
8484
[`display()`](../../api/#pyscriptdisplay) should write their values.
85-
* `terminal` - A traditional [terminal](terminal.md) is shown on the page.
85+
* `terminal` - a traditional [terminal](terminal.md) is shown on the page.
8686
As with conventional Python, `print` statements output here. **If the
8787
`worker` flag is set the terminal becomes interactive** (e.g. use
8888
the `input` statement to gather characters typed into the terminal by the
8989
user).
90+
* `service-worker` - an optional attribute that allows you to slot in a custom
91+
[service worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API)
92+
(such as `mini-coi.js`) to fix header related problems that limit the use
93+
of web workers. This rather technical requirement is fully explained in
94+
the [section on web workers](../workers/#option-2-service-worker-attribute).
9095

9196
!!! warning
9297

docs/user-guide/workers.md

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ unresponsive. **You should never block the main thread.**
77
Happily, PyScript makes it very easy to use workers and uses a feature recently
88
added to web standards called
99
[Atomics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics).
10-
You don't need to know about Atomics to use web workers, but the underlying
11-
[coincident library](architecture.md#coincident)
10+
**You don't need to know about Atomics to use web workers**, but it's useful to
11+
know that the underlying [coincident library](architecture.md#coincident)
1212
uses it under the hood.
1313

1414
!!! info
1515

16-
Sometimes you only need to `await` in the main thread the result of a call
17-
to a method exposed in a worker, where neither `window` nor `document` are
18-
either imported or ever needed in such worker.
16+
Sometimes you only need to `await` in the main thread on a method in a
17+
worker when neither `window` nor `document` are referenced in the code
18+
running on the worker.
1919

20-
In these cases, you don't need any special header or *Service Worker*
21-
as long as any worker exposed utility **returns a serializable
22-
result from the method called on the worker**.
20+
In these cases, you don't need any special header or service worker
21+
as long as **the method exposed from the worker returns a serializable
22+
result**.
2323

2424
## HTTP headers
2525

26-
For synchronous Atomics operations to work, those that enable features
27-
such as `window` and `document` from a worker, **you must ensure your web server
28-
enables the following headers** (this is the default behavior for
26+
To use the `window` and `document` objects from within a worker (i.e. use
27+
synchronous Atomics) **you must ensure your web server enables the following
28+
headers** (this is the default behavior for
2929
[pyscript.com](https://pyscript.com)):
3030

3131
```
@@ -35,21 +35,21 @@ Cross-Origin-Embedder-Policy: require-corp
3535
Cross-Origin-Resource-Policy: cross-origin
3636
```
3737

38-
If you need those features and you are not able to configure your server's headers,
39-
there are at least 2 options: use the
40-
[mini-coi](https://github.com/WebReflection/mini-coi#readme) project
41-
to enforce server side headers on each request or
42-
use the `service-worker` attribute.
38+
If you're unable to configure your server's headers, you have two options:
39+
40+
1. Use the [mini-coi](https://github.com/WebReflection/mini-coi#readme) project
41+
to enforce headers.
42+
2. Use the `service-worker` attribute with the `script` element.
4343

4444
### Option 1: mini-coi
4545

46-
This is still a preferred option, mostly for performance reasons, so that
47-
*Atomics*' synchronous operations can work at native speed.
46+
For performance reasons, this is the preferred option so Atomics works at
47+
native speed.
4848

4949
The simplest way to use mini-coi is to copy the
5050
[mini-coi.js](https://raw.githubusercontent.com/WebReflection/mini-coi/main/mini-coi.js)
51-
file content and save it in the root of your website (i.e. `/`), and reference it
52-
as the first child tag in the `<head>` of your HTML documents:
51+
file content and save it in the root of your website (i.e. `/`), and reference
52+
it as the first child tag in the `<head>` of your HTML documents:
5353

5454
```html
5555
<html>
@@ -61,23 +61,27 @@ as the first child tag in the `<head>` of your HTML documents:
6161
</html>
6262
```
6363

64-
### Option 2: service-worker attribute
64+
### Option 2: `service-worker` attribute
65+
66+
This allows you to slot in a custom
67+
[service worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API)
68+
to handle requirements for synchronous operations.
6569

66-
Recently introduced, each `<script type="m/py">` or `<m/py-script>` can have
67-
a `service-worker` attribute that must point to a locally served file, the
68-
same way `mini-coi.js` needs to be served, but there is more to it:
70+
Each `<script type="m/py">` or `<m/py-script>` may optionally have
71+
a `service-worker` attribute pointing to a locally served file (the
72+
same way `mini-coi.js` needs to be served).
6973

70-
* you can chose `mini-coi.js` itself or *any other Service Worker*,
71-
as long as it provides either the right headers to enable *Atomics*
72-
synchronous operations ir it enables
73-
[sabayon polyfill events](https://github.com/WebReflection/sabayon?tab=readme-ov-file#service-worker)
74-
* you can copy and paste
75-
[sabayon Service Worker](https://raw.githubusercontent.com/WebReflection/sabayon/main/dist/sw.js)
76-
into your local project and point at that in the attribute. This will
77-
not change the original behavior of your project, it will not interfere with
78-
all default or pre-defined headers your application use already but it will
79-
fallback to a (slower but working) synchronous operation that would enable
80-
both `window` and `document` access whenever that's needed in your worker logic
74+
* You can chose `mini-coi.js` itself or *any other custom service worker*,
75+
as long as it provides either the right headers to enable synchronous
76+
operations via Atomics, or it enables
77+
[sabayon polyfill events](https://github.com/WebReflection/sabayon?tab=readme-ov-file#service-worker).
78+
* Alternatively, you can copy and paste the
79+
[sabayon Service Worker](https://raw.githubusercontent.com/WebReflection/sabayon/main/dist/sw.js)
80+
into your local project and point at that in the attribute. This will
81+
not change the original behavior of your project, it will not interfere with
82+
all default or pre-defined headers your application uses already but it will
83+
**fallback to a (slower but working) synchronous operation** that allows
84+
both `window` and `document` access in your worker logic.
8185

8286
```html
8387
<html>
@@ -94,15 +98,14 @@ same way `mini-coi.js` needs to be served, but there is more to it:
9498
</html>
9599
```
96100

97-
!!! note
101+
!!! warning
98102

99-
Using the *sabayon* fallback to enable *Atomics* synchronous operations
100-
should be the least solution to consider because it is inevitably
101-
slower than having native operations enabled by other means.
103+
Using sabayon as the fallback for synchronous operations via Atomics
104+
should be **the last solution to consider**. It is inevitably
105+
slower than using native Atomics.
102106

103-
When that is still needed or desired, it is always better to reduce
104-
the amount of synchronous operations by caching references from the
105-
*main* thread.
107+
If you must use sabayon, always reduce the amount of synchronous
108+
operations by caching references from the *main* thread.
106109

107110
```python
108111
# ❌ THIS IS UNNECESSARILY SLOWER
@@ -128,9 +131,9 @@ same way `mini-coi.js` needs to be served, but there is more to it:
128131
print(dataset.test)
129132
```
130133

131-
In latter example the amount of operations have been reduced
132-
from *6* to just *4* and the rule of thumb is:
133-
if you ever need a *DOM* reference more than once, cache it 👍
134+
In latter example the number of operations has been reduced from six to just
135+
four. The rule of thumb is: _if you ever need a DOM reference more than once,
136+
cache it_. 👍
134137

135138

136139
## Start working

0 commit comments

Comments
 (0)