adafruit / Adafruit_CircuitPython_Wiznet5k

Pure-Python interface for WIZNET 5k Ethernet modules
Other
15 stars 35 forks source link

Socketpool #158

Closed justmobilize closed 4 months ago

justmobilize commented 4 months ago

This is an attempt to create a real SocketPool class and so the interface (radio) can be set per-pool.

Old style:

import board
import busio
import digitalio
import adafruit_requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as pool

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"

cs = digitalio.DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
eth = WIZNET5K(spi_bus, cs)
pool.set_interface(eth)
requests = adafruit_requests.Session(pool)
print(requests.get(TEXT_URL).text)

New style:

import board
import busio
import digitalio
import adafruit_requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
from adafruit_wiznet5k.adafruit_wiznet5k_socketpool import SocketPool

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"

cs = digitalio.DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
eth = WIZNET5K(spi_bus, cs)
pool = SocketPool(eth)  # this sets the interface in the pool, thus making code easer and similar to built-in
requests = adafruit_requests.Session(pool)
print(requests.get(TEXT_URL).text)
justmobilize commented 4 months ago

@anecdata Thought I would try something. This should work with your multi-internet, using the new method.

anecdata commented 4 months ago

Invalid socket for TLS ...Is this branched from main or #157 ?

justmobilize commented 4 months ago

@anecdata from main

anecdata commented 4 months ago

Non-TLS case works fine... router shows the proper LAN IP address in sequence with the code execution. w00t!

justmobilize commented 4 months ago

Nice!

anecdata commented 4 months ago

Oh, after running for a bit, start getting:

Traceback (most recent call last):
  File "adafruit_requests.py", line 514, in _get_socket
  File "adafruit_wiznet5k_class/adafruit_wiznet5k_socketpool.py", line 198, in socket
  File "adafruit_wiznet5k_class/adafruit_wiznet5k_socketpool.py", line 234, in __init__
  File "adafruit_wiznet5k_class/adafruit_wiznet5k.py", line 629, in get_socket
RuntimeError: All sockets in use.

Once that happens, no more requests work on that link.

Same behavior with only one Ethernet link.

I'm not using ConnectionManager in that code, but could try that to see if it handles the pool better.

justmobilize commented 4 months ago

What's the code you are using? The pool isn't managed (just like a built-in one isn't). CM should help if you pass that pool in. You would need to use the new branch that allows for tracking multiple...

anecdata commented 4 months ago

The RuntimeError: All sockets in use. was after catching and ignoring a timeout exception, but somehow I can't replicate that today. Instead I get this sequence on a timeout:

Traceback (most recent call last):
  File "code.py", line 37, in <module>
  File "adafruit_requests.py", line 591, in get
  File "adafruit_requests.py", line 537, in request
  File "adafruit_wiznet5k_pr158/adafruit_wiznet5k_socketpool.py", line 274, in wrapper
  File "adafruit_wiznet5k_pr158/adafruit_wiznet5k_socketpool.py", line 484, in recv
  File "adafruit_wiznet5k_pr158/adafruit_wiznet5k_socketpool.py", line 274, in wrapper
  File "adafruit_wiznet5k_pr158/adafruit_wiznet5k_socketpool.py", line 588, in recv_into
timeout: timed out
----------------------------------------
Traceback (most recent call last):
  File "code.py", line 37, in <module>
  File "adafruit_requests.py", line 591, in get
  File "adafruit_requests.py", line 525, in request
  File "adafruit_connection_manager.py", line 226, in get_socket
RuntimeError: Socket already connected to...

But, it happens with the existing library too (Scenario "A" in the code below), so it's not unique to this PR (Scenario "B" in the code below).

Code... ```py import time import traceback import board import busio import digitalio import adafruit_connection_manager import adafruit_requests # A - from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K # B - #from adafruit_wiznet5k_pr158.adafruit_wiznet5k import WIZNET5K #from adafruit_wiznet5k_pr158.adafruit_wiznet5k_socketpool import SocketPool time.sleep(3) # wait for serial URL = "http://wifitest.adafruit.com/testwifi/index.html" cs = digitalio.DigitalInOut(board.A3) spi = board.SPI() radio = WIZNET5K(spi, cs) # A - pool = adafruit_connection_manager.get_radio_socketpool(radio) # B - #pool = SocketPool(radio) requests = adafruit_requests.Session(pool, None) while True: # eth._debug = True try: print("-" * 40) with requests.get(URL, timeout=15) as r: print(r.text) except Exception as ex: traceback.print_exception(ex, ex, ex.__traceback__) time.sleep(3) ```