Christoph Acs
Christoph Acs

Reputation: 51

Correct way to use flask socketio, eventlet with multiple multiprocces

I want to know if am going the right way how to work with flask socketio, eventlet with multiple multiprocces.

I have multiple procces to handle camera streams with gstreamer, each proc write frames and meta data to redis.

My main programm use socketio to handle this procs. (start, stop) and getting data from the redis, and send it over sockets to a webclient.

Code below are very reduced but show what i did. In generally code works, but i want to get sure if I do it the right way. Please let me know if more Information needed.

Big thanks


app = Flask(__name__)
socket_io = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')

class DeviceProcess(mp.Process):

    def __init__(self, stop_event=None):
        super().__init__()
        self.stop_event = stop_event

    def run(self):
        """
            Worker 
        """
        while self.stop_event.is_set() == False:
            #do gstreamer stuff and store data to redis

class DeviceManager(object):

    def __init__(self):
        """
            Handles device proc's
        """
        self.device_procs = []
        self.pill2kill = multiprocessing.Event()

    def start(self):
        """
            Start all device procs
        """
        self.device_procs = []
        for device in self.recv_config.streams:
            s = DeviceProcess(
                stop_event=self.pill2kill
            )
            s.start()
            self.device_procs.append(s)
        
    def stop(self):
        """
            Stop all device proc's
        """
        self.pill2kill.set()

@socket_io.on('stream_details')
def stream_details(data):
    while True:
        """
           Getting data from redis and emit 
        """
        emit('data',  {<redis_data>})
        # Stream with 25-FPS
        socket_io.sleep(1 / 25)

@socket_io.on('stop_devices')
def stop_devices():
    """
       Stop all devices procs via socket-oi
    """
    app.dm.stop_procs()
    emit('data',  {'action': 'done'})

@socket_io.on('start_devices')
def start_devices():
    """
       Start all devices procs via socket-oi
    """
    app.dm.pill2kill.clear()
    app.dm.start()
    emit('data',  {'action': 'done'})

if __name__ == '__main__':
    app.dm = DeviceManger()
    app.dm.start()
    socket_io.run(app)

Upvotes: 1

Views: 233

Answers (0)

Related Questions