【问题标题】:Bottle Microframework does not close the socket once is closed一旦关闭,Bottle Microframework 不会关闭套接字
【发布时间】:2012-12-15 01:15:50
【问题描述】:

我用bottle和python运行了一个API RestFul,一切正常,API是一个在系统中运行的守护进程,如果我通过命令行停止守护进程,服务会很好地停止并关闭所有端口和连接,但是当我通过 API 关闭服务,端口在 LISTEN 状态下保持活动状态,然后在 TIME_WAIT 状态下保持活动状态,它不会释放端口。我已经阅读了两天,但问题是因为瓶子有一个插座,它不能很好地关闭服务器,但我可以找到他的解决方案

关闭API即服务的代码是python这样启动的子进程

@get('/v1.0/services/<id_service>/restart')
def restart_service(id_service):
try:
    service = __find_a_specific_service(id_service)
    if(service == None or len(service) < 1):
        logging.warning("RESTful URI: /v1.0/services/<id_service>/restart " +    id_service +" , restart a specific service, service does not exists")
        response.status = utils.CODE_404
        return utils.convert_to_json(utils.FAILURE, utils.create_failed_resource(utils.WARNING, utils.SERVICES_API_SERVICE_NOT_EXIST))
    else:
        if id_service != "API":
            api.ServiceApi().restart(id_service)
        else:
            import subprocess                
            args='/var/lib/stackops-head/bin/apirestd stop; sleep 5; /var/lib/stackops-head/bin/apirestd start'
            subprocess.Popen(args, shell=True)
        logging.info("RESTful URI: /v1.0/services/<id_service>/restart " + id_service +" , restart a specific service, ready to construct json response...")
        return utils.convert_to_json(utils.SERVICE, None)
except Exception, e:
    logging.error("Services: Error during the process of restart a specific service. %r", e)
    raise HTTPError(code=utils.CODE_500, output=e.message, exception=e, traceback=None, head

【问题讨论】:

    标签: python sockets rest popen bottle


    【解决方案1】:

    要从外部终止瓶子进程,请发送 SIGINT。

    【讨论】:

      【解决方案2】:

      如果应用程序退出或终止,所有文件描述符/句柄(包括套接字)都会被操作系统关闭。

      你也可以使用

      sudo netstat -anp --tcp
      

      在 Linux 中确定指定端口是否为某些进程所拥有。或者使用

      netstat -a -n -b -p tcp
      

      在 Windows 中做同样的事情。

      TIME_WAIT 是由操作系统而不是应用程序管理的正常状态,用于保持连接/端口一段时间。有时它很烦人。您可以调整操作系统保持多长时间,但这并不安全。

      【讨论】:

        猜你喜欢
        • 2013-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 2011-05-08
        相关资源
        最近更新 更多