瓶子Microframework一旦关闭就不会关闭套筒

时间:2012-09-25 10:04:33

标签: python sockets rest popen bottle

我使用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

2 个答案:

答案 0 :(得分:1)

要从外部终止瓶子处理,请发送SIGINT。

答案 1 :(得分:0)

如果app退出或被杀死,所有文件描述符/句柄也包括套接字都会被OS关闭。

您也可以使用

sudo netstat -anp --tcp
在Linux中

以确保指定的端口是否由某些进程拥有。或者使用

netstat -a -n -b -p tcp
在Windows中

做同样的事情。

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