Write function gets skipped within while loop

I get sensor data from a UDP socket (blocking) and would like to display changes on Sreamlit.
I’m trying to display thoes changes with the function write() from Streamlit.

For some reason the function place.write(data) gets only executed every third time.
Meanwhile the logging.info(f'data: {data}') gets execueted every time.

Expectation:
The write() function prints every time the logging function prints something out.

Code:

import socket
import streamlit as st
import time
import logging
import sys

UDP_IP = '127.0.0.1'
UDP_PORT = 1623
RUN = 1

st.header('Listening')
logging.info('run')

@st.cache
def bind_socket(ip,port):
    logging.info('bind socket')
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # UDP
    sock.bind((ip, port))
    return sock

place = st.empty()

sock = bind_socket(UDP_IP, UDP_PORT)

if st.button('stop'):
    logging.info('stop')
    RUN = 0

def main():
    global RUN
    try:
        while RUN:
            time.sleep(0.2)
            logging.info('try')
            data, addr = sock.recvfrom(65535)
            place.write(data)
            logging.info(f'data: {data}')
    except Exception :
        logging.info(f'except: {repr(Exception)} sys: {sys.exc_info()}')
    finally:
        logging.info('finally')
        sock.close()

if __name__ == "__main__":
    main()

Output of logger:
socket_logger

Output Streamlit: (changed place.write(data) to st.write(data)
socket_streamlit

Does someone know why it skypes write() and how to fix it?

Hi @Felix_Bartschi, welcome to the community! :wave: :partying_face:

What version of Streamlit are you using? I was unable to reproduce the behavior with Streamlit 1.7.0. Both logging.info() and st.write() get executed for every iteration of the while loop :thinking:

Here’s a simple UDP client I used, along with your identical app:

udp-client.py

# udp-client.py
import socket
import time

destination = ("127.0.0.1", 1623)
udpClientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

ctr = 0
while True:
    ctr += 1
    time.sleep(0.2)
    udpClientSocket.sendto(f"{ctr}".encode(), destination)

streamlit_app.py

# streamlit_app.py
import socket
import streamlit as st
import time
import logging
import sys

UDP_IP = '127.0.0.1'
UDP_PORT = 1623
RUN = 1

st.header('Listening')
logging.info('run')

@st.cache
def bind_socket(ip,port):
    logging.info('bind socket')
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # UDP
    sock.bind((ip, port))
    return sock

sock = bind_socket(UDP_IP, UDP_PORT)

if st.button('stop'):
    logging.info('stop')
    RUN = 0

def main():
    global RUN
    try:
        while RUN:
            time.sleep(0.2)
            logging.info('try')
            data, addr = sock.recvfrom(65535)
            st.write(data)
            logging.info(f'data: {data}')
    except Exception :
        logging.info(f'except: {repr(Exception)} sys: {sys.exc_info()}')
    finally:
        logging.info('finally')
        sock.close()

if __name__ == "__main__":
    main()

Output

Output is identical to expected behavior: The write() command prints every time the logging function prints something out:

write-skipped-while

Best, :balloon:
Snehan

Hey @snehankekre thanks for the fast replay.
I solved the problem but the behavior remains strange.
I’m using Anaconda with
Streamlit V1.5.0
Python V3.9

Just updatet to Streamlit V1.7.0 the issue remains.
I’m running your udp-client.py code.
I figured out by using another browser i’m getting a different output.
Running two browsers parallel Chrome and Brave while running the code the output looks like:
Chrome:
chrome
Brave:
brave

The Issue:
When opening multiple tabs or browsers with the same streamlit address (http://localhost:8501/) the write() function distributes the output to only one of the tabs (like round robin). resulting in skipping displaying values.

Solution:
Close all other tabs of Streamlit in the browser. :slight_smile: