0

I ran an API RestFul with bottle and python, all works fine, the API is a daemon running in the system, if I stop the daemon by command line the service stop very well and closed all the port and connections, but when I go to close the service through the API, the port keep alive in state LISTEN and later in TIME_WAIT, it does not liberate the port. I've read for two days but the problem is because bottle have a socket and it does not close the server well, but I can find he solution

The code to close the API as a service is a subprocess launched by python like this

@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
4

2 回答 2

1

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

于 2012-09-25T23:17:14.270 回答
0

如果应用程序被退出或杀死,所有文件描述符/句柄也包括套接字被操作系统关闭。

你也可以使用

sudo netstat -anp --tcp

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

netstat -a -n -b -p tcp

在 Windows 中做同样的事情。

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

于 2012-12-14T17:15:50.260 回答