【问题标题】:Python Flask REST API freezes during a call to subprocess commandPython Flask REST API 在调用子进程命令期间冻结
【发布时间】:2020-03-24 09:52:02
【问题描述】:

我们有一个有效的 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()

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

【问题讨论】:

  • Flask 异步运行。因此,您有一个线程。也许this 有帮助。

标签: python flask subprocess


【解决方案1】:

问题是:

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

更具体:

shell=True

等待进程停止并读取

标准输出

作为一种快速解决方法,安装 gunicorn 应用并提供标志 --timeout 120 --workers 20

所以现在 1 名工作人员忙,19 名仍在返回其他请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-11
    • 2018-08-02
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多