Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socketpool #158

Closed
wants to merge 2 commits into from
Closed

Socketpool #158

wants to merge 2 commits into from

Conversation

justmobilize
Copy link
Collaborator

@justmobilize justmobilize commented Apr 21, 2024

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
Copy link
Collaborator Author

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

@anecdata
Copy link
Member

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

@justmobilize
Copy link
Collaborator Author

@anecdata from main

@anecdata
Copy link
Member

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

@justmobilize
Copy link
Collaborator Author

Nice!

@anecdata
Copy link
Member

anecdata commented Apr 21, 2024

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
Copy link
Collaborator Author

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
Copy link
Member

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...
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)

@justmobilize justmobilize deleted the socketpool branch April 23, 2024 20:25
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