首页 > 解决方案 > Running flask-socketio in thread

问题描述

I have an interactive Python application where I also want to use flask-socketio to interface with Javascript clients. Hence, I need Python socketio to run as a thread.

Approach #1:

def socketio_server_fn():
    socketio.run(flask_app, port=5000)
flask_app = Flask(__name__)
socketio = flask_socketio.SocketIO(flask_app, always_connect=True, async_mode='threading')
socketio_thread = socketio.start_background_task(socketio_server_fn)

Problem 1.1: It doesn't use the Websocket transport as that's not compatible with standard Python threads. Instead, socketio falls back to polling.

Problem 1.2: Polling is not only inefficient but also spams the console with messages such as these every second:

127.0.0.1 - - [10/Oct/2019 13:57:11] "GET /socket.io/?EIO=3&transport=polling&t=MsrXwsJ&sid=c63dfaefdbb84c688dd53bef2f6d3c77 HTTP/1.1" 200 -

I haven't been able to remove these messages and others have had very mixed results too: Disable console messages in Flask server

Approach #2: Use eventlet

First we need to patch the Python threading system at the start of the program:

import eventlet
eventlet.monkey_patch()

And then change the SocketIO object creation line to:

socketio = flask_socketio.SocketIO(flask_app, always_connect=True, async_mode='eventlet')

Now socketio uses Websocket.

Problem 2.1: I would prefer to avoid monkey_patch for the whole Python threading system.

Problem 2.2: Using eventlet seems to break socketio.stop() when tearing down the application. stop() now hangs even when called from a HTTP handler function. The same teardown code worked with both async_mode='threading' and using normal socketio.run(). socketio.server.stop() also hangs.

Problem 2.3: Eventlet doesn't seem fully compatible with prompt_toolkit:

Exception in default exception handler
Traceback (most recent call last):
  File "python\lib\site-packages\prompt_toolkit\eventloop\win32.py", line 102, in _run_task
    t()
  File "python\lib\site-packages\prompt_toolkit\eventloop\context.py", line 116, in new_func
    return func(*a, **kw)
  File "python\lib\site-packages\prompt_toolkit\patch_stdout.py", line 103, in write_and_flush_in_loop
    run_in_terminal(write_and_flush, in_executor=False)
  File "python\lib\site-packages\prompt_toolkit\application\run_in_terminal.py", line 50, in run_in_terminal
    return run_coroutine_in_terminal(async_func, render_cli_done=render_cli_done)
  File "python\lib\site-packages\prompt_toolkit\application\run_in_terminal.py", line 71, in run_coroutine_in_terminal
    assert app._is_running
AssertionError

Approach #3: Using gevent This doesn't seem to work at all.

Isn't there an easy solution to the seemingly common use case of running flask-socketio as a thread?

标签: pythonflasksocket.ioeventlet

解决方案


由于这个问题得到了赞成,我可以提到我通过跳出框框思考解决了这个难题。我改为 aiohttp 而不是 flask- socketio

我的解决方案是这样的(不完整的代码):

from aiohttp import web

class MyThreadedServer(Thread):

    def __init__(self):
        self._loop = None  # The event loop for the async web server

    async def _on_shutdown(self, app):
        for conn in set(self.connections):
            await conn.close()

    def run(self):
        #Runs a separate asyncio loop on this (separate) thread
        self._loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self._loop)
        self.stop_server = asyncio.Event()
        self._loop.run_until_complete(self._run_app())
        #Or in Python 3.7, simply:
        #  asyncio.run(run())
        self._loop.close()

    async def _run_app(self):
        self.app = web.Application()
        self.app.on_shutdown.append(self._on_shutdown)    
        runner = web.AppRunner(self.app)
        await runner.setup()
        site = web.TCPSite(runner, self.hostname, self.port)
        await site.start()
        print('Started web server on %s port %d' % (self.hostname, self.port))
        await self.stop_server.wait()
        print('Web server closing down')

    def stop(self):
        "Call from any thread"
        # Overrides EventThread.stop() to change implementation
        if self.stop_server:
            self._loop.call_soon_threadsafe(self.stop_server.set)

推荐阅读