From d00f32a70c8af91aae9d7afd02752fca77db8fe0 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 14 Oct 2024 13:57:42 +0200 Subject: [PATCH] More tidying worker docs --- docs/usage/webworker.md | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/docs/usage/webworker.md b/docs/usage/webworker.md index a2792c1347d7..f81e0f08d254 100644 --- a/docs/usage/webworker.md +++ b/docs/usage/webworker.md @@ -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: @@ -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.