Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
python socket-io emit() doesn't send time until after execution

Using the python socketio package, I created a timer that starts when a client connects to my server.

Server-side code:

import socketio

sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files={
    '/': './public/'
})


@sio.event
def connect(sid, environ):
    count = 10
    
    while count > 0:
        sio.emit('timer_count', count)
        sio.sleep(1)
        count -= 1

HTML code for reference (index.html):

<!doctype html>
<html>
    <head>
        <title>Timer</title>
    </head>
    <body>
        <h1>Timer Build</h1>
        <p1 id="counter"></p1>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.2/socket.io.min.js"></script>
        <script src="/index.js"></script>
    </body>
</html>

Client-side JS code (index.js):

const sio = io();

sio.on('timer_count', function(count) {
    console.log(count);
    var s = document.getElementById("counter");
    s.innerHTML = count;
});

However, the behavior I am getting is that the data isn't emitted to the client until it appears the server side code is finished executing (i.e. the whole count is printed all at once). How can I get this to behave in a true timer fashion where the console.log() function prints the data (count) every second?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have added your loop in the connect event handler, which is used to accept or reject the connection. The Socket.IO connection is not going to be fully established until you return from this handler.

Move your loop to a separate event that executes after the connect handler returns and accepts the connection.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda