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

2024-09-28 20:51:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我们有一个可工作的RESTAPI,用于处理前端的许多差异端点。在子流程命令应处理操作的一个特定端点期间,所有其他端点将停止并等待子流程完成。有人能帮我理解为什么会这样吗?我认为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()

所有其他端点的工作速度都很快,没有任何延迟。谢谢你的帮助


Tags: jsondatagetreturnifcountaccounterror
1条回答
网友
1楼 · 发布于 2024-09-28 20:51:12

问题是:

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

更具体地说:

shell=True

等待进程停止并读取

STDOUT

作为一种快速解决方法,安装了gunicorn应用程序,并带有一个标志 timeout 120 workers 20

因此,现在1名工作人员正忙,19名仍在返回其他请求

相关问题 更多 >