select.poll not working properly with sys.stdin
Port, board and/or hardware
MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico with RP2040
MicroPython version
When using sys.stdin with select.poll and calling poll() or ipoll() to wait for available data, only sys.stdin.read(1) returns while sys.stdin.read() blocks.
However, reading only character by character is probably very inefficient and blows up code additionally.
Reproduction
import sys, select
import sys, select
p = select.poll()
p.register(sys.stdin, select.POLLIN)
while True:
for o, _ in p.ipoll():
if o is sys.stdin:
read = sys.stdin.read()
print('read', read)
Expected behaviour
sys.stdin.read() should immediately return when sending data to the serial interface on the PC the Pico is connected to.
Observed behaviour
sys.stdin.read() still blocks.
The same behavior when using sys.stdin.buffer instead of sys.stdin.
The code works when using sys.stdin.read(1) instead of sys.stdin.read()
Additional Information
No, I've provided everything above.
Code of Conduct
Yes, I agree
select.poll not working with ssl.wrap_socket on Arduino Nano Connect or UM Feather S2
When using select.poll on an ssl.wrap_socket object the first poll call returns a value but the next poll returns an empty list when there are still data in the stream.
The two boards I tested that had the issue:
MicroPython v1.19.1 on 2022-06-18; FeatherS2 with ESP32-S2
MicroPython v1.19.1-746-gf2de289ef on 2022-12-13; Arduino Nano RP2040 Connect with RP2040
I also tested on a Pico W but it worked as expected:
MicroPython v1.19.1-776-gbb77c1d5a on 2022-12-16; Raspberry Pi Pico W with RP2040
The code I used to test is:
from os import uname
import sys
import ssl
import usocket as _socket
import network
import select
PORT = 443
HOST = "api.weather.gov"
QUERY = "/gridpoints/BOX/70,76/forecast"
url = "https://"+HOST+QUERY
wlan = network.WLAN(network.STA_IF)
if not wlan.active():
wlan.active(True)
if not wlan.isconnected():
wlan.connect('myssid','password')
wlan.isconnected()
wlan.ifconfig()[0]
addr = _socket.getaddrinfo(HOST, PORT)[0][4]
client = _socket.socket()
client.connect(addr)
response = ssl.wrap_socket(client)
headers = {"user-agent": "RetiredWizard@"+sys.implementation.name.lower()+uname()[2]}
response.write('GET %s HTTP/1.1\r\nHost: %s\r\nuser-agent: %s\r\n\r\n'%(QUERY,HOST,headers['user-agent']))
poller = select.poll()
poller.register(response, select.POLLIN)
res = poller.poll(10000)
read_response = ""
kount = 0
print(res)
while res:
kount += 1
print(kount)
read_response += response.read(1).decode()
res = poller.poll(10000)
The S2 and Nano Connect boards disply the following:
[(<_SSLSocket 3f7b78a0>, 1)]
1
and then return to the REPL after the 10 second timeout. The Pico W on the other hand displays the socket information and then the counter counts up to the number of bytes in the stream (12,000+).