简体   繁体   English

Python 3-Flask Restful API重新启动另一个API

[英]Python 3 - Flask Restful API to restart another API

I created a python REST web-service, and it will execute a series of subprocess commands to restart another python REST webservice. 我创建了一个python REST Web服务,它将执行一系列子过程命令来重新启动另一个python REST Web服务。 But the problem is it doesn't really start my other REST service after I call it. 但是问题是,在我调用它之后并没有真正启动我的其他REST服务。

Here is my code for the REST API : 这是我的REST API代码:

from flask import Flask, jsonify, request, make_response
from flask_restful import fields, reqparse, abort, Api, Resource
import sqlalchemy
from sqlalchemy.sql import table, column, select, update, insert, delete, func
from sqlalchemy.orm import sessionmaker
import requests
import time
import traceback
import subprocess
import os

app = Flask(__name__)
api = Api(app)

class RestartPython(Resource):
    def get(self):

        command1 = 'fuser -k 9876/tcp'
        proc1 = subprocess.Popen(command1, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
        proc1.wait()

        print('kill python process done')

        command2 = 'source activate envpy3'
        proc2 = subprocess.Popen(command2, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
        proc2.wait()

        print('switch python env done')

        command3 = 'python ReceiveCall.py'
        print(command3)
        proc3 = subprocess.Popen(command3, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
        proc3.wait()

        print('ReceiveCall started')

        return make_response(jsonify({'message': 'Python webservice is restarted', 'status': 'Ok'}), 200)

    api.add_resource(RestartPython, '/restart')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8082, debug=True)

This will works if I just run it without wrapping them in web-service 如果我只运行它而不将它们包装在Web服务中,那么它将起作用

import subprocess

if __name__ == '__main__':
    command1 = 'fuser -k 9876/tcp'
    proc1 = subprocess.Popen(command1, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
    proc1.wait()

    print('kill python process done')

    command2 = 'source activate envpy3'
    proc2 = subprocess.Popen(command2, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
    proc2.wait()

    print('switch python env done')

    command3 = 'python ReceiveCall.py'
    print(command3)
    proc3 = subprocess.Popen(command3, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
    proc3.wait()

    print('ReceiveCall started')

Any ideas? 有任何想法吗?

instead of wait() i would recommend to use communicate() . 而不是wait(),我建议使用communication()。 communicate is used to avoid deadlock wherever pipe is used . 使用communication可以避免在使用管道的任何地方出现死锁。 Let me know if below modification works for you 让我知道下面的修改是否对您有用

from flask import Flask, jsonify, request, make_response
from flask_restful import fields, reqparse, abort, Api, Resource
import sqlalchemy
from sqlalchemy.sql import table, column, select, update, insert, delete, func
from sqlalchemy.orm import sessionmaker
import requests
import time
import traceback
import subprocess
from subprocess import PIPE,Popen
import os

app = Flask(__name__)
api = Api(app)

class RestartPython(Resource):
    def get(self):

        command1 = 'fuser -k 9876/tcp'
        proc1 = subprocess.Popen(command1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        #proc1.wait()
        (stdout, stderr) = proc1.communicate()

        print '.'.join(stdout)
        print '.'.join(stderr)

        print('kill python process done')

        command2 = 'source activate envpy3'
        proc2 = subprocess.Popen(command2, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
        #proc2.wait()
        proc2.communicate()

        print('switch python env done')

        command3 = 'python ReceiveCall.py'
        print(command3)
        proc3 = subprocess.Popen(command3, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
        #proc3.wait()
         proc3.communicate()

        print('ReceiveCall started')

        return make_response(jsonify({'message': 'Python webservice is restarted', 'status': 'Ok'}), 200)

    api.add_resource(RestartPython, '/restart')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8082, debug=True)

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

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