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

Add socket/sessions cleanup method to API #7

Closed
RetiredWizard opened this issue Apr 19, 2024 · 2 comments · Fixed by #13
Closed

Add socket/sessions cleanup method to API #7

RetiredWizard opened this issue Apr 19, 2024 · 2 comments · Fixed by #13

Comments

@RetiredWizard
Copy link

This issue appeared while using esp32SPI WiFi connections.

In my application I will often fire off some code that will perform some internet requests but then needs to clean everything up to free up space and resources. Depending on what the user does, the internet code may be fired up again or a different chuck of code that uses the same pin/spi resources my be run. Before connection manager I would simply deinit and close everything I could between uses which still mostly works but I've needed to add adafruit_connection_manager._global_socketpool.clear() to my list of deinits.

As the _global_socketpool is technically private data it would be nice if there was a "proper/official" API to get this done.

This is the code snippet I've been using to test the issue
from digitalio import DigitalInOut
import busio
import board
import os
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_connection_manager
import adafruit_requests

text_url = "https://www.moneycontrol.com/us-markets"

esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
#radio = adafruit_esp32spi.ESP_SPIcontrol(spi,esp32_cs,esp32_ready,esp32_reset,debug=4)
radio = adafruit_esp32spi.ESP_SPIcontrol(spi,esp32_cs,esp32_ready,esp32_reset,debug=False)
print(f'Connected: {radio.is_connected}')
while not radio.is_connected:
    radio.connect_AP(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
print(radio.pretty_ip(radio.ip_address))
pool = adafruit_connection_manager.get_radio_socketpool(radio)
requests = adafruit_requests.Session(pool,adafruit_connection_manager.get_radio_ssl_context(radio))
response = requests.get(text_url,headers=None,timeout=15000)


radio.disconnect()
response.close()
radio = None
esp32_cs.deinit()
esp32_ready.deinit()
esp32_reset.deinit()
spi.deinit()
requests = None
adafruit_connection_manager._global_socketpool.clear()
#pool.socket.close()
pool = None


esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
radio = adafruit_esp32spi.ESP_SPIcontrol(spi,esp32_cs,esp32_ready,esp32_reset,debug=False)
print(f'Connected: {radio.is_connected}')
while not radio.is_connected:
    radio.connect_AP(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
print(radio.pretty_ip(radio.ip_address))
pool = adafruit_connection_manager.get_radio_socketpool(radio)
requests = adafruit_requests.Session(pool,adafruit_connection_manager.get_radio_ssl_context(radio))
print(f'esp32_ready: {esp32_ready.value}')
response = requests.get(text_url,headers=None,timeout=15000)
@justmobilize
Copy link
Collaborator

@RetiredWizard there are a few things that would need to be cleaned up. I've opened #8, which is unrelated but want to get that in first before I add a method to close everything

@justmobilize
Copy link
Collaborator

@dhalbert does this seem right to you:

I'm thinking:

  • a adafruit_connection_manager.close_all(socket_pool) which would get the connection manager for that socket_pool
  • call _free_sockets(force) (the addition of force would close all, even if still open)

And if you didn't pass in a socket_pool, it would iterate over all the connection managers (see PR linked above)

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 a pull request may close this issue.

2 participants