简体   繁体   English

使用 gevents Wsgi 服务器在进程中多次启动和停止烧瓶应用程序

[英]Start and Stop a flask app multiple times within a process using gevents Wsgi server

I have a flask app which I need to start and then shut down within the same process and repeat this multiple times.我有一个烧瓶应用程序,我需要在同一进程中启动然后关闭它并多次重复此操作。 I am using gevents in my application so I'm using gevents.pywsgi as my WSGI server.我在我的应用程序中使用 gevents,所以我使用gevents.pywsgi作为我的 WSGI 服务器。 Now I'm trying to gracefully shutdown the server so I can restart it in the same process after I do a few other things.现在,我正在尝试正常关闭服务器,以便在执行其他一些操作后可以在同一进程中重新启动它。

So the following code creates a basic flask app, which when receives a POST request with any valid data on /hit endpoint populates the data field.因此,以下代码创建了一个基本的 Flask 应用程序,当接收到带有 /hit 端点上任何有效数据的 POST 请求时,它会填充数据字段。 A greenlet is running in parallel with this app and when it sees that the data field is populated, it shuts down the server.一个 greenlet 与这个应用程序并行运行,当它看到数据字段被填充时,它会关闭服务器。

    def func():
    global data
    data = None

    app = Flask(__name__)

    @app.route('/hit', methods=['POST'])
    def hit():
        global data
        data = request.json

        if data is not None:
            return "Input Recieved, Server closed "
        else:
            return "Invalid Input, Try again"

    def shutdown_server(_server):
        global data
        while data is None:
            sleep(0.5)
        _server.stop()
        _server.close()

    server = WSGIServer(('0.0.0.0',5100), app)

    start = spawn(server.start)
    stop = spawn(shutdown_server, request, server)

    joinall([start, stop])

    return True

Now this code is running fine if I run the server once, but if I try to run the server again within the same process it throws the following error:现在,如果我运行服务器一次,此代码运行良好,但是如果我尝试在同一进程中再次运行服务器,则会引发以下错误:

Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 766, in gevent._greenlet.Greenlet.run
  File "/home/batman/Documents/genisys/lib/python3.6/site-packages/gevent/baseserver.py", line 308, in start
    self.start_accepting()
  File "/home/batman/Documents/genisys/lib/python3.6/site-packages/gevent/baseserver.py", line 160, in start_accepting
    self._watcher = self.loop.io(self.socket.fileno(), 1)
AttributeError: 'WSGIServer' object has no attribute 'socket'

I'm not sure if this is even possible, or if I have to run the server in a separate process if I want to run it multiple times.我不确定这是否可行,或者如果我想多次运行它是否必须在单独的进程中运行服务器。 Can anybody tell me why I'm facing this error and is there a better and cleaner way to shutdown the server so I don't face this error?谁能告诉我为什么我会遇到这个错误,有没有更好更干净的方法来关闭服务器,这样我就不会遇到这个错误?

EDIT : I give IP and port as arguments to func, so I have tried call func with a different port multiple times and with the same port multiple times, I still get the same error.编辑:我将 IP 和端口作为 func 的参数,所以我尝试多次使用不同的端口调用 func 并多次使用相同的端口,但我仍然遇到相同的错误。

我认为问题是您已经在端口 5100 上运行服务器,因此您无法在同一端口上运行进程,因此您必须在启动进程时动态更改端口号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM