@@ -7,25 +7,25 @@ unresponsive. **You should never block the main thread.**
77Happily, PyScript makes it very easy to use workers and uses a feature recently
88added 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 )
1212uses 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
3535Cross-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
4949The 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