Flask-SocketIO错误 - 在Windows服务中超时

时间:2018-03-22 09:29:11

标签: python flask flask-socketio

我尝试使用Flask-SocketIO创建Windows服务。我正在使用Python 2.7.13和Flask-SocketIO 2.8.6开发Windows 10 Enterprise。

我能够创建名为"TestFlaskService"的Windows服务。但是当我启动这个Windows服务时,Flask-SocketIO服务正在启动,我可以访问浏览器。

我甚至可以停止Windows服务并停止Flask-SocketIO Service

但问题是Flask-SocketIO仅通过Windows服务运行15分钟,Windows服务停止运行。 Flask-SocketIO正在投掷 ERROR - timed out。我已经将追溯结束了。

以下是Windows服务的代码。

WindowsServiceCreation.py

from run import run_service

from multiprocessing import Process
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import os, signal
import subprocess

import logging

logging.basicConfig(
    filename='Windows-Service.log',
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestFlaskService"
    _svc_display_name_ = "TestFlaskService"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None,0,0,None)
        socket.setdefaulttimeout(60)
        self.server = None

    def SvcStop(self):
        try:
            logging.info("Initiated Stopping....")
            pid = self.getPID()
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            logging.info("Service Stop pending done...!")
            win32event.SetEvent(self.hWaitStop)

            if pid:
                self.ReportServiceStatus(win32service.SERVICE_STOPPED)
                logging.info("Got PID : " + str(pid))
                logging.info("+++++++++++++++ Server Stopped +++++++++++++++")
                os.kill(pid, signal.SIGTERM)
                logging.info("Killed the process")
            else:
                logging.info("+++++++++++++++ NO PID FOUND FOR GIVEN PORT "+self.getPort()+" +++++++++++++++")

        except Exception as e:
            logging.exception(e)


    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        try:
            logging.info("---------------- Starting Server ----------------")
            self.server = Process(target = run_service())
            #below lines are not being executed. Server is started in above line itself.
            self.server.start()
            self.server.run()
            logging.info("---------------- Server started ----------------")
        except Exception as e:
            logging.exception(e)

    def getPort(self):
        from app.utils.config import server_port
        return str(server_port)

    def getPID(self):
        port = self.getPort()
        command_line = 'netstat -a -n -o | find "' + port + '"'
        print command_line
        result = ''
        process = subprocess.Popen(command_line,
                                   shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        errcode = process.returncode
        if errcode is None:
            for line in process.stdout:
                if ('0.0.0.0:' + port in line or '127.0.0.1:' + port) and '0.0.0.0:0' in line and 'LISTENING' in line:
                    result += line
        if 'LISTENING' and port in result:
            parts = result.split()
            return int(parts[-1])
        else:
            return None


if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)

run.py

import eventlet
eventlet.monkey_patch()
from app import app
from app.utils.config import server_ip, server_port, is_debug_enabled
from app.celery_app import celery
from app.utils import Logger

from app.celery_app import socketio


def run_service():
    Logger.Infolog('Applog', 'Starting Server...')
    socketio.init_app(app)
    print "Running with", socketio.async_mode
    Logger.Infolog('Applog', "Running with " + socketio.async_mode)
    socketio.run(app, host=server_ip, port=server_port)

if __name__ == '__main__':
    run_service()

堆栈跟踪

2018-03-22 12:47:02,855 - ERROR - timed out
Traceback (most recent call last):
  File "D:\Projects\AFA\AFA\WindowsService.py", line 68, in main
    run_service()
  File "D:\Projects\AFA\AFA\run.py", line 26, in run_service
    socketio.run(app, host=server_ip, port=server_port)
  File "C:\Anaconda2\lib\site-packages\flask_socketio\__init__.py", line 495, in run
    run_server()
  File "C:\Anaconda2\lib\site-packages\flask_socketio\__init__.py", line 490, in run_server
    log_output=log_output, **kwargs)
  File "C:\Anaconda2\lib\site-packages\eventlet\wsgi.py", line 896, in server
    client_socket = sock.accept()
  File "C:\Anaconda2\lib\site-packages\eventlet\greenio\base.py", line 224, in accept
    self._trampoline(fd, read=True, timeout=self.gettimeout(), timeout_exc=_timeout_exc)
  File "C:\Anaconda2\lib\site-packages\eventlet\greenio\base.py", line 207, in _trampoline
    mark_as_closed=self._mark_as_closed)
  File "C:\Anaconda2\lib\site-packages\eventlet\hubs\__init__.py", line 163, in trampoline
    return hub.switch()
  File "C:\Anaconda2\lib\site-packages\eventlet\hubs\hub.py", line 295, in switch
    return self.greenlet.switch()
timeout: timed out

编辑1: 我有猴子修补了eventlet库,代码在run.py文件中更新。仍然,错误保持不变。我做错了吗?

0 个答案:

没有答案