Python Flask REST API在调用子流程命令期间冻结

问题描述 投票:0回答:1

我们有一个有效的REST API,可处理FrontEnd的许多差异端点。在一个特定的端点上,其中子进程命令应处理操作,所有其他端点都将暂停,并等待子进程完成。谁能帮助我了解它为什么会发生?我认为Python Flask异步运行?

[...]

class withdrawCrypto(Resource):
    def get(self):
        auth = json.loads('{"ERROR" : "User authentication failed!"}')
        wrongAmount = json.loads('{"ERROR" : "Wrong amount"}')
        wrongWlt = json.loads('{"ERROR" : "Invalid wallet provided. Please check the wallet addr!"}')
        notEnough = json.loads('{"ERROR" : "You don\'t have enough crypto to withdraw this amount"}')
        account = str(request.args.get('account'))
        token = str(request.args.get('token'))
        wallet = str(request.args.get('wlt'))
        count = int(request.args.get('count'))

        if len(wallet) != 34:
            return jsonify(data=wrongWlt)

        if wallet[0] != 'B':
            return jsonify(data=wrongWlt)

        cursorLG.execute("select balance from btc WHERE login=%s;", account)
        checkBalance = cursorLG.fetchall()

        if checkBalance[0]['balance'] < int(count):
            return jsonify(data=notEnough)

        cursorLG.execute("select cred from accounts WHERE login=%s;", account)
        userCheck = cursorLG.fetchall()

        if userCheck[0]['secret'] == token:
            if count and int(count) > 0:
                host = credentials['rpc']
                user = credentials['rpcuser']
                passwd= credentials['rpcpassword']
                timeout = credentials['rpcclienttimeout']
                command = 'bitcoin-cli -rpcconnect=' + host + ' -rpcuser=' + user + ' -rpcpassword=' + passwd  + ' -rpcclienttimeout=' + timeout + ' sendtoaddress ' + wallet + ' ' + str(count)
                result = subprocess.check_output(command,shell=True).strip()

                cursorLG.execute("select balance from btc WHERE login=%s", account)
                current = cursorLG.fetchall()

                setNew = int(int(current[0]['balance']) - int(count))

                cursorLG.execute("replace into btc (login, balance, lastwithdrawalwlt) values (%s, %s, %s) ", (account, setNew, wallet))
                return jsonify(data=result.decode("utf-8"))
            else:
                return jsonify(data=wrongAmount)
        else:
            print('Failed Crypto withdrawal! Actual passw / user sent: ', userCheck[0]['secret'], token)
            return jsonify(data=auth)

[...]

# Serve the high performance http server
if __name__ == '__main__':
    http_server = WSGIServer(('', 9000), app)
    http_server.serve_forever()

所有其他端点都可以快速工作,没有任何延迟。任何帮助表示赞赏。

python flask subprocess
1个回答
0
投票

问题是:

result = subprocess.check_output(command,shell=True).strip()

更具体:

shell=True

等待进程停止并读取

STDOUT

作为已安装gunicorn应用程序并带有标志--timeout 120 --workers 20的快速解决方法

因此,现在有1名工人很忙,有19名工人仍在返回其他请求。

© www.soinside.com 2019 - 2024. All rights reserved.