Prime Number Calculator with Workers
Run the app | View the code on GitHub
A demonstration of PyScript workers showing how to keep the main thread responsive whilst performing heavy computation in a background worker.
- Enter a number (10 to 100,000).
- Click "Find Primes".
- Watch the green heartbeat - it never stops pulsing.
- See primes appear in real-time.
Try interacting with the page whilst it's computing - everything stays smooth because the main thread is never blocked.
What it demonstrates
- Worker architecture:
- Main thread: MicroPython (lightweight, fast startup, responsive UI).
- Worker thread: Pyodide with numpy (heavy computation, numerical libraries).
- A clear separation of concerns.
- Key patterns:
- Starting a worker from the main thread.
- Calling worker methods with
await. - Sending incremental results back via callbacks.
- Using
pyscript.syncto expose functions between threads. - Keeping the main thread responsive during computation.
- Visual feedback:
- Animated "heartbeat" proves main thread never blocks.
- Real-time display of primes as they're found.
- Status updates showing worker progress.
Features
- MicroPython on main thread:
- Fast startup (no heavy packages to load).
- Lightweight (perfect for UI interactions).
- Stays responsive (no blocking operations).
- Pyodide in worker:
- Full Python with scientific libraries (numpy).
- Heavy computation off the main thread.
- Can use the full Python ecosystem.
- Best of both worlds:
- Fast, responsive UI.
- Powerful computation when needed.
- Users never see a frozen interface.
Files
index.html- Page structure and styling.main.py- Main thread logic (MicroPython).worker.py- Worker thread logic (Pyodide with numpy).worker-config.json- Worker configuration (numpy package).
How it works
Main thread (MicroPython)
The main thread handles the user interface:
- Gets reference to the worker via
pyscript.workers. - Registers a callback function (
handle_prime) viapyscript.sync. - Calls the worker's
find_primes()method when the button is clicked. - Receives prime numbers via the callback and updates the display.
- Stays responsive throughout (watch the pulsing green dot).
Worker thread (Pyodide)
The worker does the heavy lifting:
- Exposes
find_primes()method via@syncdecorator. - Uses numpy's efficient array operations for the Sieve of Eratosthenes.
- Calls back to the main thread's
handle_prime()for each prime found. - Sends results in batches with small delays to keep UI smooth.
- Returns a summary when complete.
Starting the worker
# Main thread gets reference to worker defined in HTML.
from pyscript import workers
worker = await workers.py # Name from script tag's type.
Calling worker methods
# Main thread calls worker method (must be decorated with @sync).
result = await worker.find_primes(10000)
Worker exposing methods
# Worker exposes method to main thread.
from pyscript import sync
@sync
async def find_primes(limit):
# Do computation.
return result