I am using pyscript to do image processing for a website I am making on hamming and reed solomon codes. It does however take a long time to load. I have a css loading animation but it freezes while the python image processing code is running. Is there a way for me to run my python scripts while still retaining HTML animation and function?
I have looked into multiprocessing and threading. Both seem unavailable in the current state of pyscript, pyodide and html. I am considering changing the css to a gif, but this doesn't fix other interactables on the website.
Python running in PyScript, just like JavaScript, is inherently limited to the single browser main thread (but see below). While the main thread is busy working on a synchronous (blocking) task, liking running a single function, all other interactivity on the page will be suspended.
The (current) way around this is to make your code either asynchronous, writing your code as a coroutine to periodically yield execution back to the Browser's event loop. For example, you could take a multi-step process and break it up with await-able events like asyncio.sleep()
:
import asyncio
async def myLongFunction():
doThing1("foo")
await asyncio.sleep(0)
doThing2("bar")
await asyncio.sleep(0)
return doThing3("baz")
asyncio.ensure_future(myLongFuction())
Simpler solutions may be available in the future. There's quite a bit of work currently on allowing PyScript to execute code in a web worker, which wouldn't block the main thread while executing, although the results must be passed as structured messages back and forth.
Additionally, having access to pthreads in Pyodide (the primary runtime underpinning PyScript) is a long-standing goal, but one that's inching closer to being possible.