Skip to content

Instantly share code, notes, and snippets.

@anecdata
Created November 14, 2023 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anecdata/d3f2e104b2cc173fed0fcf4bdeb96158 to your computer and use it in GitHub Desktop.
Save anecdata/d3f2e104b2cc173fed0fcf4bdeb96158 to your computer and use it in GitHub Desktop.
CircuitPython asyncio w/ Requests iter_content()
import random
import board
import wifi
import socketpool
import ssl
import neopixel
import asyncio
import adafruit_requests
# Choose your own URL adventure...
URL = "https://anecdata.dev/ada/chunked3/"
async def iter(req):
while True:
print(f"\nGET {URL}")
with req.get(URL) as resp:
print(resp.status_code, resp.reason.decode(), resp.headers)
for c in resp.iter_content():
print(f"{c.decode()}", end="")
await asyncio.sleep(0)
async def pixel():
with neopixel.NeoPixel(board.NEOPIXEL, 1) as pixels:
while True:
pixels.fill(random.randrange(0, 256*256*256))
await asyncio.sleep(0.1)
async def main():
iter_task = asyncio.create_task(iter(req))
neo_task = asyncio.create_task(pixel())
await asyncio.gather(iter_task, neo_task)
pool = socketpool.SocketPool(wifi.radio)
req = adafruit_requests.Session(pool, ssl.create_default_context())
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment