adafruit / Adafruit_CircuitPython_ESP32SPI

ESP32 as wifi with SPI interface
MIT License
103 stars 75 forks source link

800 bps when serving webpage with WSGIServer #174

Closed rivques closed 2 years ago

rivques commented 2 years ago

Hi! I've been messing around with a Metro M4 Airlift Lite and hit an interesting snag when trying to serve a webpage using Adafruit's esp32spi_wsgiserver and adafruit_wsgi.wsgi_app. (Code is here for those interested.) When serving the webpage, I was getting only about 800 bits per second, slow enough to cause Chrome to have issues recognizing the format of the page as HTML because (I assume) the <!DOCTYPE html> did not arrive quickly enough. After digging through debug=True logs and the various modules of adafruit_esp32spi, it seems like the issue is that WSGIServer.finish_response is sending data to ESP_SPIcontrol.socket_write one byte at a time. This bypasses socket_write's built-in chunking and seems to cause a lot of extra overhead, possibly due to #108. To fix this I use a slightly modified version of esp32spi_wsgiserver where I socket_write the entire response at once if possible instead of looping over every char in the response and writing it individually. This increased data rate to something like 178 kbps. Anyways, my questions are:

  1. Am I missing something that would cause this to be a non-issue?
  2. Is this a known issue and if so, is there another workaround?
  3. Would this problem be fixed by #108 being resolved?
  4. Is my workaround a reasonable one, or does it have pitfalls I can't see? If so, should I put it in a PR to this repo?
dhalbert commented 2 years ago

My intention in the near future is to remove esp32spi_wsgiserver entirely and recommend that people use generic server implementations that use adafruit_requests, of which there are several. This makes the library smaller and allows us to write better server implementations that can be used in more circumstances. The PR to do that is #167, which is stalled for other reasons.

dhalbert commented 2 years ago

Your fix sounds perfectly reasonable for now, and we can get it in for posterity.