[Python] How do I get the threading model right with sanic and pywebview?

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I build lots of little python apps - I like web app uis, I like sanic as a backend because I'm mostly async these days... but I also want it to feel like a native app - ie its own dedicated window, the App closes when I close the window - and when I open another web page it doesn't join that page.

    I found pywebview.. and it seems perfect - and in fact it is - if I make a little runner that starts up my separate sanic python program with sh python blah.py - it works great.

    However - this all falls down if I try to put them in the same program. Sanic seems to only work if it's the main thread - if you try to start it with asyncio or something it just has all sorts of random issues - the same applies to pywebview.

    This gets worse - because in the sort of Apps I'm doing I typically am displaying some status or something, and have some polling going on - so I have other thread or threads going on watching things - that I want want then send the status of to the UI.

    Whatever I try - I just cannot get them working reliably together - ie pywebview, sanic, and multiple polling threads. And the problems are many and numerous, depending what I try - either sanic doesn't respond, or pywebview doesn't respond, none of them shut down properly, things line up, I get errors about event loops and the like. I get crashes in one of the threads when I try to exit... I just can't figure it out.

    Can anyone explain exactly how to do it with complete reliability? Ie have

    • a pywebview
    • talking to a sanic server
    • which is being fed information from various background polling threads
    • information that then gets sent to the ui via web sockets

    It seems like it should be really common and simple :(

    Continue reading...

Compartilhe esta Página