0

I'm creating HMI web application which visualise realtime data from simulation.

It means I'm updating data every 0.25/0.5 seconds. I decided to use socket.io.

So I want my server after connection to emit data every some period of time. I thought the best option would be emitting data in something similar like setInterval in JavaScript. However, in python this has not proved too easy. I tried a lot of options from stackoverflow f.e:

Python Equivalent of setInterval()?

But most of them are causing errors. Here are some methods which I tried.

@socketio.on('signalList')
def start_polling_signals():
    global poll_signals
    poll_signals = threading.Timer(1000, start_polling_signals())
    poll_signals.start()
    list_signals_v2()
    print('polling')

@socketio.on('stopSignalList')
def stop_polling_signals():
    global poll_signals
    poll_signals.cancel()
    poll_signals = None

Causes maximum recursion depth exceeded

@socketio.on('signalList')
def start_polling_signals():
    starttime = time.time()
    while True:
        list_signals_v2()
        time.sleep(1 - ((time.time() - starttime) % 1))

Causes that other socket events don't work, and I have no idea how to stop that polling.

Any ideas how to do it in optimal way? Have in mind I need to be able to start and stop the interval.

dymek662
  • 112
  • 9

1 Answers1

0

You should start a background task using the

start_background_task(target, *args, **kwargs)

This is the correct way to init something like what you want to do. Pass as target a function that will send data in the period of time you want it to send data.