Skip to content

Add a Conway’s-Game-of-Life example#14

Merged
erikrose merged 4 commits into
mainfrom
game-of-life-example
Oct 28, 2025
Merged

Add a Conway’s-Game-of-Life example#14
erikrose merged 4 commits into
mainfrom
game-of-life-example

Conversation

@erikrose
Copy link
Copy Markdown
Member

@erikrose erikrose commented Oct 28, 2025

This shows off some of Fastly’s strengths w.r.t. Python in a plain visual way:

  • Quick spin-up of workers
  • Quick network transfer

I imagine it's more representative of customer use cases than a heavy web app: quick computations that make a decision and ditch. Plus, blinkenlights!

@erikrose erikrose force-pushed the game-of-life-example branch from af4c97a to ebff6e3 Compare October 28, 2025 16:00
Hits the worker for every frame, which will perhaps be impressive.
…oard to 100x100.

This lets us get boards over 44x44 to work in Viceroy, which has some URL-length limits.
@erikrose erikrose force-pushed the game-of-life-example branch from ebff6e3 to f0d840f Compare October 28, 2025 16:17
28fps vs. 9

Other measurements, for posterity, on my M3 Max:
57fps: Flask, 44x44, compression
34fps: Viceroy, 44x44, compression
34fps Viceroy, 44x44, no compression
The above 2 show compression doesn't have a speed effect. So we keep it in case network IO is slow. Or in case someone is on MS Edge, which has a 2048b URL-length limit.

40fps: Flask, 100x100, compression
9fps: Viceroy, 100x100, compression
@erikrose erikrose force-pushed the game-of-life-example branch from f0d840f to 4b604ea Compare October 28, 2025 16:19
@erikrose erikrose marked this pull request as ready for review October 28, 2025 16:21
@erikrose erikrose requested a review from posborne October 28, 2025 16:21
Comment thread examples/game-of-life.py

@app.route("/")
def root():
return """<!DOCTYPE html>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I was considering options for moving this out to a separate file -- I wonder if it could be worthwile as a feature to have wasiless (or a separate step using wasi-virt) allow for doing fs virtualization and bundling a directory and its contents to some mount point in the image.

Python would use wasi fs but the virtualization layer would satisfy whatever it tried to access via its bundling.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to look into whether importlib.resources could be made to work reasonably with componenize-py. That would be the ultimate in Pythonicness.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Experimenting with this. So far, it looks promising. componentize-py is happy to accept packages.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No luck with the naive approach. The files in the package aren't around, and there's no automatic workaround. Even trying to read the __init__.py that's cited in a traceback reveals that it isn't really there. The results of resources.read_text("game_of_life", "__init__.py"):

[2025-10-29 15:45:16,944] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/2/flask/app.py", line 1511, in wsgi_app
  File "/2/flask/app.py", line 919, in full_dispatch_request
  File "/2/flask/app.py", line 917, in full_dispatch_request
  File "/2/flask/app.py", line 902, in dispatch_request
  File "/0/game_of_life/__init__.py", line 113, in root
  File "/python/importlib/resources/_functional.py", line 32, in read_text
  File "/python/pathlib/__init__.py", line 792, in read_text
  File "/python/pathlib/__init__.py", line 776, in open
FileNotFoundError: [Errno 44] No such file or directory: '/0/game_of_life/__init__.py'

Copy link
Copy Markdown
Member

@posborne posborne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Game board state to evolve in the path seems a bit hard to deal with but has the obvious benefit of potentially giving us starting seeds of a sort we could use as examples.

I could see it being nice to have a later version of this allow for setting a fixed frame rate for more consistent animation with the option of going full beans.

@erikrose
Copy link
Copy Markdown
Member Author

Thanks for the quick review! The framerate is actually capped at whatever the browser chooses to do, typically 60Hz. But sure, one could add a slider or what-have-you.

@erikrose
Copy link
Copy Markdown
Member Author

Putting game board state in the URL is mainly a nod to semantics: GET is conventionally cacheable, idempotent, not expected to mutate any state, etc. Unfortunately, it doesn't permit any request body, which is where I would rather have put the big board data! I experimented with PUT, which has weird base semantics for this case but the same idempotency, but I ran into (probably solveable) trouble with the JS API. Everything but Edge has plenty of URL space for the board at up to around 256x256, so I let it lie for now.

@erikrose erikrose merged commit f1bd043 into main Oct 28, 2025
3 checks passed
@erikrose erikrose deleted the game-of-life-example branch October 29, 2025 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants