Created
November 14, 2023 16:22
-
-
Save anecdata/d3f2e104b2cc173fed0fcf4bdeb96158 to your computer and use it in GitHub Desktop.
CircuitPython asyncio w/ Requests iter_content()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
https://discordapp.com/channels/327254708534116352/537365702651150357/1169290671962718218