DOC update docs/usage/webworker (#1298)

Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
This commit is contained in:
Mireille Raad 2021-03-03 11:03:43 -07:00 committed by GitHub
parent 16c3514b7e
commit 4a770ffb0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 8 deletions

View File

@ -18,7 +18,7 @@ Update the `webworker.js` sample so that it has as valid URL for `pyodide.js`, a
`self.languagePluginUrl` to the location of the supporting files. `self.languagePluginUrl` to the location of the supporting files.
In your application code create a web worker `new Worker(...)`, In your application code create a web worker `new Worker(...)`,
and attach listeners to it using its `.onerror` and [`.onmessage`][onmessage] and attach listeners to it using its `.onerror` and `.onmessage`
methods (listeners). methods (listeners).
Communication from the worker to the main thread is done via the `Worker.postMessage()` Communication from the worker to the main thread is done via the `Worker.postMessage()`
@ -31,22 +31,20 @@ method (and vice versa).
In this example process we will have three parties involved: In this example process we will have three parties involved:
* The **web worker** is responsible for running scripts in its own thread separate thread. * The **web worker** is responsible for running scripts in its own separate thread.
* The **worker API** exposes a consumer-to-provider communication interface. * The **worker API** exposes a consumer-to-provider communication interface.
* The **consumer**s want to run some scripts outside the main thread so they don't block the main thread. * The **consumer**s want to run some scripts outside the main thread so they don't block the main thread.
### Consumers ### Consumers
Our goal is to run some Javascript code in another thread, this other thread will Our goal is to run some Python code in another thread, this other thread will
not have access to the main thread objects. Therefore we will need an API that takes not have access to the main thread objects. Therefore we will need an API that takes
as input not only the Python `script` we wan to run, but also the `context` on which as input not only the Python `script` we wan to run, but also the `context` on which
it relies (some Javascript variables that we would normally get access to if we it relies (some Javascript variables that we would normally get access to if we
were running the Python script in the main thread). Let's first describe what API were running the Python script in the main thread). Let's first describe what API
we would like to have. we would like to have.
Here is an example of consumer that will exchange with the , via the worker Here is an example of consumer that will exchange with the web worker, via the worker interface/API `py-worker.js`. It runs the following Python `script` using the provided `context` and a function called `asyncRun()`.
interface/API `py-worker.js` to run the following Python `script` using the provided `context`
using a function called `asyncRun()`.
```js ```js
import { asyncRun } from './py-worker'; import { asyncRun } from './py-worker';
@ -79,11 +77,11 @@ main();
``` ```
Before writing the API, lets first have a look at how the worker operates. Before writing the API, lets first have a look at how the worker operates.
How does our web worker will run the `script` using a given `context`. How does our web worker run the `script` using a given `context`.
### Web worker ### Web worker
[A worker][worker API] is ... Let's start with the definition. [A worker][worker API] is:
> 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. This context is represented by either a DedicatedWorkerGlobalScope object (in the case of dedicated workers - workers that are utilized by a single script), or a SharedWorkerGlobalScope (in the case of shared workers - workers that are shared between multiple scripts). > 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. This context is represented by either a DedicatedWorkerGlobalScope object (in the case of dedicated workers - workers that are utilized by a single script), or a SharedWorkerGlobalScope (in the case of shared workers - workers that are shared between multiple scripts).