RPI-Python-Sockets: socket still see packets after network stream ends. Do they get buffered?

my application requires me to detect multicast streams and react to them. Defining the sockets and igmp is fine but once the stream(s) ends my program is still reacting to packets it “thinks?” sees even though all streams have finished.

I thought maybe the IDE output was getting bogged down but minimum logging and running code from CLI has the same problem.

Ive written this code a few different ways now and THIS issue of having some “queue” of packets having to be processed is messing things up.

Did I miss somthing?

This iteration I’m using the select libray in Python (3.9) to detect if the sockets have data like this

   readable, _, _ = select.select(sockets, [], [], 0)  

The output never seems to go back to zero i.e. no more active data on sockets … or am I missing the point on how to use selection to detect this traffic.

===============================

import socket
import struct
import time
import select

multicast_addresses = {'224.0.1.10': [50002, 50003,50010,50024]}

sockets = []
active_ports=[]

active_MC ={
            "50002":[False],"50003":[False],
            "50010":[False],"50024":[False]
            }

for multicast_address, ports in multicast_addresses.items():
    for port in ports:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((multicast_address, port))
        group = socket.inet_aton(multicast_address)
        mreq = struct.pack('4sL', group, socket.INADDR_ANY)
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
        #sock.setblocking(False)
        sockets.append(sock)
        
#print (sockets)

while True:
    # List of sockets to monitor for read events
    readable, _, _ = select.select(sockets, [], [], 0)  # Timeout set to 1 second
    print("just after readable", readable)
    
    # If any of the multicast sockets are in the readable list, it means there's incoming data
    #print(f'the lenght of readable : {len(readable)} {readable}')

    #print(readable,end='/r')
    #print (f"streams dected = {len(readable)}")
    #print(active_MC)
    port_check = '50003'
    length = len(readable)
    pass

    for sock in readable: # Create List of active ports
        port = sock.getsockname()[1] # get the name of one of the ports
        active_ports.append(port)
        print(f"active_ports array = {active_ports} type={type(active_ports)} {type(active_ports[0])}")        

        #note active_ports get stored as integers
        
    #print("After For loop acive Ports:",active_ports)

    #for port_check in active_MC.keys(): # loop through all ports defined
        #print ("Ports For loop read ",port_check,type(port_check)) 
        #print(f"Array Port_check = {port_check} ")

        # port_check is a str
    if int(port_check) in active_ports : # stream is active if true
        #print(f"Vars in first test port_check= {port_check} active_MC = {active_MC[str(port_check)][0]}")

        if active_MC[port_check][0] == False :# First time detected
            print(f"stream started on port: {port_check}nn")
            active_MC[port_check][0] = True
            print("MAIN: ",active_MC)
        else:
            # True Events may be ignored or Not
            # thinking I may need to time duration of stream
            #print("Failed First Test")
            pass

    else:
        print("main if statement was falsen",port_check,active_MC[port_check][0])
        # here Port is NOT an active stream so set to false and log
        if active_MC[port_check][0] == True :# Stream was live but no longer
            print(f"stream STOPPED on port: {port_check}")
            active_MC[port_check][0] = False
            print("POST: ",active_MC)
        else:
            print("POST if statement was falsen")
            # True Events may be ignored or Not
            # thinking I may need to time duration of stream
            pass
        
    active_ports.clear() # clear the array for the next text
    time.sleep(1)
        

    
    
            

I expect that if there is no data being received by the socket then the select statement will not return that socket to the list of active sockets that are recieving streams.

I have a seperate box that generates the MC streams on the correct port to generate the events

In a previous generation of this code I setup 10 different threads each listening to one unique socket. I pretty sure that caused a memory leak cause I had crashes every 38 days from a boot (almost exactly) once that was implemented. I figured a more efficient way is required.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật