Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pyodide
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
pyodide
Commits
38b2fdef
Commit
38b2fdef
authored
Jan 31, 2019
by
Jason Stafford
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add documentation explaining how to use the webworker
parent
e1fb1b96
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
0 deletions
+65
-0
docs/index.md
docs/index.md
+1
-0
docs/using_pyodide_from_webworker.md
docs/using_pyodide_from_webworker.md
+63
-0
src/webworker.js
src/webworker.js
+1
-0
No files found.
docs/index.md
View file @
38b2fdef
...
...
@@ -3,6 +3,7 @@
## Using Pyodide
-
[
Using Pyodide directly from Javascript
](
using_pyodide_from_javascript.md
)
-
[
Using Pyodide from a web worker
](
using_pyodide_from_webworker.md
)
-
[
Using Pyodide from Iodide
](
using_pyodide_from_iodide.md
)
## Developing Pyodide
...
...
docs/using_pyodide_from_webworker.md
0 → 100644
View file @
38b2fdef
# Using Pyodide from a web worker
This document describes how to use pyodide to execute python scripts
asynchronously in a web worker.
## Startup
Setup your project to server
`webworker.js`
. You should also serve
`pyodide.js`
, and all its associated
`.asm.js`
,
`.data`
,
`.json`
, and
`.wasm`
files as well, though this is not strictly required if
`pyodide.js`
is pointing
to a site serving current versions of these files. (Currently the default URL
of https://iodide.io/pyodide-demo/ contains outdated versions).
Update
`webworker.js`
so that it has as valid URL for
`pyodide.js`
, and sets
`self.languagePluginUrl`
to the location of the supporting files.
In your application code create a web worker, and add listeners for
`onerror`
and
`onmessage`
.
Call
`postMessage`
on your web worker, passing and object with the key
`python`
containing the script to execute as a string. You may pass other keys in the
data object. By default the web worker assigns these to its global scope so that
they may be imported from python. The results are returned as the
`results`
key,
or if an error was encountered, it is returned in the
`error`
key.
For example:
```
var pyodideWorker = new Worker('./webworker.js')
pyodideWorker.onerror = (e) => {
console.log(`Error in pyodideWorker at ${e.filename}, Line: ${e.lineno}, ${e.message}`)
}
pyodideWorker.onmessage = (e) => {
const {results, error} = e.data
if (results) {
console.log('pyodideWorker return results: ', results)
} else if (error) {
console.log('pyodideWorker error: ', error)
}
}
const data = {
A_rank: [0.8, 0.4, 1.2, 3.7, 2.6, 5.8],
python:
'import statistics\n' +
'from js import A_rank\n' +
'statistics.stdev(A_rank)'
}
pyodideWorker.postMessage(data)
```
## Loading packages
Packages referenced from your python script will be automatically downloaded
the first time they are encountered. Please note that some of the larger
packages such as
`numpy`
or
`pandas`
may take several seconds to load.
Subsequent uses of these packages will not incur the download overhead of the
first run, but there is still some time required for the
`import`
in python
itself.
src/webworker.js
View file @
38b2fdef
self
.
languagePluginUrl
=
'
http://localhost:8000/
'
importScripts
(
'
./pyodide.js
'
)
var
onmessage
=
function
(
e
)
{
// eslint-disable-line no-unused-vars
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment