Skip to content

webworkers - initial startup helper聽#207

Description

@Neon22

New docs are looking great. 馃憤
Was looking through webworkers and thought you might want to add a starter function which might be used when the page starts up to pre-initialise the workers so they are ready sooner.

I took this from a prior code version so it probably needs some refining, updating to 2026.1.1, and to be in a function
but something like this could be useful to add

# start workers but no handles to use and have to wait - so use ltk.schedule instead
for el in window.document.querySelectorAll("[type='py'][worker][name]"):
    print("found",el, el.id)
    print(workers, workers.id, workers.name)
    el.onload(ltk.proxy(lambda e: print("loaded",e)))
    print(dir(el))
    await workers[el.getAttribute('name')]

Activity

  1. WebReflection commented on Jan 22, 2026

    @WebReflection
    Contributor

    @Neon22 I am not sure el.onload makes sense in there, because the script is handled by JS, not by the browser, load event in there might not be triggering the way you expect.

    On top of that, it looks like this way you still await one worker after the other, which is OKish but I'd rather do it this way:

    envs = []
    
    for el in window.document.querySelectorAll("[type='py'][worker][name]"):
        print("found",el, el.id)
        print(workers, workers.id, workers.name)
        el.onload(ltk.proxy(lambda e: print("loaded",e)))
        print(dir(el))
        envs.append(workers[el.getAttribute('name')])
    
    import js
    await js.Promise.all(envs)

    this would await in parallel all workers, not one after the other, so if the order does not matter, that's the way I'd go.

  2. Neon22 commented on Jan 22, 2026

    @Neon22
    ContributorAuthor

    Seems much better than what I cobbled together from an earlier build.

    Please consider adding it to the docs.
    I think it would be useful to be able to kick the Workers off before needing them, so that the "app" would be even more responsive.

    Cheers...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions