Ejecting from a stream using the socketio flash drive extension

I want to send a delayed message to a socket client. For example, when a new client connects, the message “check started” should be sent to the client, and after certain seconds another message should be selected from the stream.

@socket.on('doSomething', namespace='/test')
def onDoSomething(data):
  t = threading.Timer(4, checkSomeResources)
  t.start()
  emit('doingSomething', 'checking is started')

def checkSomeResources()
  # ...
  # some work which takes several seconds comes here
  # ...
  emit('doingSomething', 'checking is done')

But the code does not work due to a context problem. I get

RuntimeError('working outside of request context')

Is it possible to emit a stream from a stream?

+4
source share
1 answer

The problem is that the thread has no context to know which user the message belongs to.

request.namespace , . :

@socket.on('doSomething', namespace='/test')
def onDoSomething(data):
    t = threading.Timer(4, checkSomeResources, request.namespace)
    t.start()
    emit('doingSomething', 'checking is started')

def checkSomeResources(namespace)
    # ...
    # some work which takes several seconds comes here
    # ...
    namespace.emit('doingSomething', 'checking is done')
+4

Source: https://habr.com/ru/post/1570004/


All Articles