Skip to content

Commit

Permalink
More tidying worker docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Oct 14, 2024
1 parent 0483e73 commit d00f32a
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions docs/usage/webworker.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
This document includes an example demonstrating how to use Pyodide to execute
Python scripts asynchronously in a web worker.

Let's start with [the definition][worker api].
Let's start with [the definition of a worker][worker api].

> A worker is an object created using a constructor (e.g. [Worker()][worker constructor])
> that runs a named JavaScript file — this file contains the code
> that will run in the worker thread; workers run in another global context that
> is different from the current window.
A lot of Python code does long running synchronous computations. Running them in
the main thread will block the UI. Using a web worker is advantageous because
the Python code runs in a separate thread from your UI and does not impact your
A lot of Python programs do long-running synchronous computations. Running them
in the main thread blocks the UI. Using a web worker is advantageous because the
Python code runs in a separate thread from your UI and does not impact your
application's responsiveness.

On the other hand, since workers run in a separate global context, you cannot
directly share globals between a worker and the main thread. In particular, a
worker cannot directly manipulate the DOM.

## Detailed example

In this example process we will have three parties involved:
Expand Down Expand Up @@ -149,11 +153,3 @@ export function asyncRun(script, context) {
[worker api]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
[worker constructor]: https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker
## Caveats
There are some limitations, however. Pyodide does not support sharing the Python
interpreter and packages between multiple workers or with your main thread.
Since workers run in a separate global context distinct from the main thread,
you cannot directly share globals between a worker and your main thread. In
particular, the worker cannot directly manipulate the DOM. Finally, although the
worker is separate from your main thread, the worker is itself single threaded.

0 comments on commit d00f32a

Please sign in to comment.