Create a Web Worker running Python code. This spawns a new worker thread that can execute Python code found at the `url`, independently of the main thread. The `**options` can be used to configure the worker. ```python from pyscript import PyWorker
(url, **options)
| 164 | js_import = _pyscript.js_import |
| 165 | |
| 166 | def PyWorker(url, **options): |
| 167 | """ |
| 168 | Create a Web Worker running Python code. |
| 169 | |
| 170 | This spawns a new worker thread that can execute Python code |
| 171 | found at the `url`, independently of the main thread. The |
| 172 | `**options` can be used to configure the worker. |
| 173 | |
| 174 | ```python |
| 175 | from pyscript import PyWorker |
| 176 | |
| 177 | |
| 178 | # Create a worker to run background tasks. |
| 179 | # (`type` MUST be either `micropython` or `pyodide`) |
| 180 | worker = PyWorker("./worker.py", type="micropython") |
| 181 | ``` |
| 182 | |
| 183 | PyWorker **can only be created from the main thread**, not from |
| 184 | within another worker. |
| 185 | """ |
| 186 | return _PyWorker(url, to_js(options)) |
| 187 | |
| 188 | # Main thread has direct access to window and document. |
| 189 | window = js |