@@ -7,25 +7,25 @@ unresponsive. **You should never block the main thread.**
7
7
Happily, PyScript makes it very easy to use workers and uses a feature recently
8
8
added to web standards called
9
9
[ 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 )
12
12
uses it under the hood.
13
13
14
14
!!! info
15
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, 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.
19
19
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**.
23
23
24
24
## HTTP headers
25
25
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
29
29
[ pyscript.com] ( https://pyscript.com ) ):
30
30
31
31
```
@@ -35,21 +35,21 @@ Cross-Origin-Embedder-Policy: require-corp
35
35
Cross-Origin-Resource-Policy: cross-origin
36
36
```
37
37
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 .
43
43
44
44
### Option 1: mini-coi
45
45
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.
48
48
49
49
The simplest way to use mini-coi is to copy the
50
50
[ 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:
53
53
54
54
``` html
55
55
<html >
@@ -61,23 +61,27 @@ as the first child tag in the `<head>` of your HTML documents:
61
61
</html >
62
62
```
63
63
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.
65
69
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).
69
73
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.
81
85
82
86
``` html
83
87
<html >
@@ -94,15 +98,14 @@ same way `mini-coi.js` needs to be served, but there is more to it:
94
98
</html >
95
99
```
96
100
97
- !!! note
101
+ !!! warning
98
102
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 .
102
106
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.
106
109
107
110
```python
108
111
# ❌ THIS IS UNNECESSARILY SLOWER
@@ -128,9 +131,9 @@ same way `mini-coi.js` needs to be served, but there is more to it:
128
131
print(dataset.test)
129
132
```
130
133
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 _ . 👍
134
137
135
138
136
139
## Start working
0 commit comments