Skip to content

Commit 53df247

Browse files
committed
Work in progress
1 parent c8d62a0 commit 53df247

File tree

5 files changed

+267
-202
lines changed

5 files changed

+267
-202
lines changed

docs/faq.md

Lines changed: 103 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,126 @@
11
# FAQ
22

3-
This page contains most common questions and "*gotchas*" asked in [our Discord channel](https://discord.com/channels/972017612454232116/972017612454232119) or within our community.
3+
This page contains the most common questions and "*gotchas*" asked on
4+
[our Discord server](https://discord.gg/HxvBtukrg2), in
5+
[our community calls](https://www.youtube.com/@PyScriptTV), or
6+
within our community.
47

5-
There are two major areas we'd like to help with, grouped here as [common errors](#common-errors) and as [common hints](#common-hints).
8+
There are two major areas we'd like to explore:
9+
[common errors](#common-errors) and [helpful hints](#helpful-hints).
610

711
## Common Errors
812

9-
This area contains most common issues our users might face due technical reasons or some misconception around the topic.
13+
These are the most common errors users of PyScript encounter.
1014

1115
### SharedArrayBuffer
1216

13-
This is not by accident the very first, and most common, error our users might encounter while developing or deploying *PyScript* projects.
17+
This is the first and most common error users may encounter with PyScript.
1418

1519
!!! failure
1620

21+
Your application doesn't run and in
22+
[your browser's console](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/What_are_browser_developer_tools)
23+
you see this message:
24+
25+
```
1726
Unable to use SharedArrayBuffer due insecure environment.
1827
Please read requirements in MDN: ...
19-
20-
The error contains [a link to MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements) but it's easy to get lost behind the amount of content provided by this topic.
21-
22-
**When**
23-
24-
This errors happens in one of these combined scenarios:
25-
26-
* the server doesn't provide the correct headers to handle security concerns or there is no *Service Worker* able to override headers, as described [in the worker page](http://127.0.0.1:8000/user-guide/workers/) and ...
27-
* there is a `worker` attribute in the *py* or *mpy* script element and the [sync_main_only](https://pyscript.github.io/polyscript/#extra-config-features) flag is not present or not `True`
28-
* there is a `<script type="py-editor">` that uses a *worker* behind the scene
29-
* there is an explicit *PyWorker* or *MPWorker* bootstrap
30-
31-
The only exception is when the `sync_main_only = True` is part of the config with the following caveats:
32-
33-
* it is not possible to manipulate the DOM or do anything meaningful on the main thread directly because *Atomics* cannot guarantee sync-like locks within *worker**main* operations
34-
* the only desired use case is to expose, from the worker, `pyscript.sync` utilities that will need to be awaited from the *main* once invoked
35-
* the worker can only *await* main related references, one after the other, so that *DX* is really degraded in case one still needs to interact with main
36-
37-
If your project simply bootstraps on the *main* thread, none of this is relevant because no *worker* would need special features.
38-
39-
**Why**
40-
41-
The only way to make `document.getElementById('some-id').value` work out of a *worker* execution context is to use these two JS primitives:
42-
43-
* **SharedArrayBuffer**, which allows multiple threads to read and / or write into a chunk of memory that is, like the name suggests, shared across threads
44-
* **Atomics**, which is needed to both `wait(sab, index)` and `notify(sab, index)` to unlock the awaiting thread
45-
46-
While a *worker* is waiting for some operation on main to happen, this is not using the CPU, it just idles until that index of the shared buffer gets notified, effectively never blocking the *main* thread, still pausing its own execution until such buffer is notified for changes.
47-
48-
As overwhelming or complicated as this might sounds, these two fundamental primitives make *main**worker* interoperability an absolute wonder in term of *DX* so that we encourage to always prefer *workers* over *main* scripts, specially when it comes to *Pyodide* related projects with its heavier bootstrap or computation abilities, yet still delivering a *main-like* development experience.
49-
50-
Unfortunately, due past security concerns and attacks to shared buffers, each server or page needs to allow extra security to prevent malicious software to also read or write into these buffers but be assured that if you own your code, your project, and you trust the modules or 3rd party code you need and use, **there are no security concerns around this topic within this project**, it's simply an unfortunate "*one rule catch all*" standard any server can either enable or disable as it pleases.
28+
```
29+
30+
The error contains
31+
[a link to MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements)
32+
but it's the amount of content provided on this topic is overwhelming.
33+
34+
#### When
35+
36+
This error happens when **the server delivering your PyScript application is
37+
incorrectly configured**. It fails to provide the correct headers to handle
38+
security concerns for web workers, or you're not using
39+
[mini-coi](https://github.com/WebReflection/mini-coi#readme) as an alternative
40+
solution. (These requirements are explored
41+
[in the worker page](../user-guide/workers#http-headers).)
42+
43+
**And** at least one of the following scenarios is true:
44+
45+
* There is a `worker` attribute in the *py* or *mpy* script element and the
46+
[sync_main_only](https://pyscript.github.io/polyscript/#extra-config-features)
47+
flag is not present or not `true`.
48+
* There is a `<script type="py-editor">` that uses a *worker* behind the
49+
scenes.
50+
* There is an explicit `PyWorker` or `MPWorker` bootstrapping somewhere in your
51+
code.
52+
53+
!!! info
54+
55+
If `sync_main_only` is `true` then interactions between the main thread and
56+
workers are limited to one way calls from the main thread to methods
57+
exposed by workers.
58+
59+
If `sync_main_only = True`, the following caveats apply:
60+
61+
* It is not possible to manipulate the DOM or do anything meaningful on the
62+
main thread **from a worker**. This is because Atomics cannot guarantee
63+
sync-like locks between a worker and the main thread.
64+
* Only a worker's `pyscript.sync` methods are exposed, and they can only be
65+
awaited from the main thread.
66+
* The worker can only `await` main thread references one after the other, so
67+
developer experience is degraded when one needs to interact with the
68+
main thread.
69+
70+
If your project simply bootstraps on the main thread, none of this is relevant
71+
because no worker requires such special features.
72+
73+
#### Why
74+
75+
The only way for `document.getElementById('some-id').value` to work in a
76+
worker is to use these two JavaScript primitives:
77+
78+
* **[SharedArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer)**,
79+
to allow multiple threads to read and / or write into a chunk of shared
80+
memory.
81+
* **[Atomics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics)**,
82+
to both `wait(sab, index)` (`sab` is a `SharedArrayBuffer`) and
83+
`notify(sab, index)` to unlock the awaiting thread.
84+
85+
While a worker waits for an operation on main to happen, it is not using the
86+
CPU. It idles until the referenced index of the shared buffer changes,
87+
effectively never blocking the main thread while still pausing its own
88+
execution until the buffer's index is changed.
89+
90+
As overwhelming or complicated as this might sounds, these two fundamental
91+
primitives make main ↔ worker interoperability an absolute wonder in term of
92+
developer experience. Therefore, we encourage folks to prefer using workers
93+
over running Python in the main thread. This is especially so when using
94+
Pyodide related projects, because of its heavier bootstrap or computation
95+
requirements. Using workers ensures the main thread (and thus, the user
96+
interface) remains unblocked.
97+
98+
Unfortunately, due to security concerns and potential attacks to shared
99+
buffers, each server or page needs to allow extra security to prevent malicious
100+
software to read or write into these buffers. But be assured that if you own
101+
your code, your project, and you trust the modules or 3rd party code you need
102+
and use, **there are less likely to be security concerns around this topic
103+
within your project**. This situation is simply an unfortunate "*one rule catch
104+
all*" standard any server can either enable or disable as it pleases.
51105

52106
### Borrowed Proxy
53107

54-
This is another classic error that might happen with listeners, timers or any other circumstance where a *Python* callback might be lazily invoked in the *JS* side of affair:
108+
This is another common error that happens with listeners, timers or in any
109+
other situation where a Python callback is lazily invoked from JavaScript:
55110

56111
!!! failure
57112

58-
Uncaught Error: This borrowed proxy was automatically destroyed at the end of a function call. Try using create_proxy or create_once_callable.
113+
Your application doesn't run and in
114+
[your browser's console](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/What_are_browser_developer_tools)
115+
you see this message:
116+
117+
```
118+
Uncaught Error: This borrowed proxy was automatically destroyed at the end of a function call.
119+
Try using create_proxy or create_once_callable.
59120
For more information about the cause of this error, use `pyodide.setDebug(true)`
121+
```
60122

61-
**When**
123+
#### When
62124

63125
This error usually happens in *Pyodide* only related project, and only if a *Python* callback has been directly passed along as *JS* function parameter:
64126

@@ -85,7 +147,7 @@ This flag tries to intercept all *Python* proxies passed to a *JS* callback and
85147

86148
The [FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry) is the primitive used to do so. It is not observable and nobody can predict when it will run to free, hence destroy, retained *Python* proxies. This means that *RAM* consumption might be slightly higher, but it's the *JS* engine responsibility to guarantee that when such *RAM* consumption is too high, that finalization registry would call and free all retained proxies, leaving room for more *RAM*.
87149

88-
**Why**
150+
#### Why
89151

90152
Most *WASM* based runtimes have their own garbage collector or memory management but when their references are passed along another programming language they cannot guarantee these references will ever be freed, or better, they lose control over that memory allocation because they cannot know when such allocation won't be needed anymore.
91153

@@ -176,7 +238,7 @@ NameError: name 'failure' isn't defined
176238

177239
The same applies when the error is shown in devtools/console where unfortunately the stack right after the error message might be a bit distracting but it's still well separated from the error message itself.
178240

179-
## Common Hints
241+
## Helpful Hints
180242

181243
This area contains most common questions, hacks, or hints we provide to the community.
182244

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
<p>Join the conversation on our
5555
<a href="https://discord.gg/HxvBtukrg2" target="_blank">discord server</a>,
5656
for realtime chat with core maintainers and fellow users of PyScript.
57+
Check out <a href="https://www.youtube.com/@PyScriptTV">our YouTube
58+
channel</a>, full of community calls and show and tells.
5759
Explore
5860
<a href="https://learning.anaconda.cloud/" target="_blank">educational</a>
5961
and

docs/user-guide/offline.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
Sometimes you want to run PyScript applications offline.
44

55
Both PyScript core and the interpreter used to run code need to be served with
6-
the application itself. Two requirements are needed to create an
7-
offline PyScript are:
6+
the application itself. The two requirements needed to create an offline
7+
version of PyScript are:
88

9-
* Download and include PyScript core.
10-
* Download and include the Python interpreters used in your application.
9+
1. Download and include PyScript core.
10+
2. Download and include the Python interpreters used in your application.
1111

1212
## Get PyScript core
1313

0 commit comments

Comments
 (0)