pyscript / pyscript/docs

Worker caveats

Open
#158 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17
Forks
20
PR merge metrics
No merged PRs in 30d

Description

I just realized, out of a one-off example in Discord, that if a worker has listeners added after some reference is created or populated with values, the synchronous dance might mislead because that reference on the main might have been already bootstrapped or triggered/resolved.

The explicit issue I am talking about is this one:

image = window.Image.new()
# if this is before, we're all good
image.onload = lambda e: document.body.append(image)
# if that is after though, setting an inline source of the image
# will resolve it before the next `.onload = ...` or even
# `.addEventListener` is attached
image.src = f'data:image/svg+xml;utf8,{svg}'

The demo in case is this one https://pyscript.com/@agiammarchi/svg-image/latest?files=main.py,index.html

I had to double-check why, once added the worker attribute, that would not work.

Due inability to hook into listeners happening on the main, I think it's worth it to document the best way to be sure a listener is triggered, either on main or worker thread, is to define listeners related to such bootstrap AOT (Ahead of Time) and not later.

This is an important detail I could see myself failing at remembering on occasions, so I think it should be part, at least, of our F.A.Q.s section.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FAQ section and review the linked SVG image demo, especially the ordering of listener registration and the inline image source. Document the listener-ordering caveat for main and worker contexts, with guidance to register bootstrap listeners ahead of time; the FAQ is done when this scenario and the recommended ordering are clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.