python函数多次调用内存溢出_python - send_from_directory函数无缘无故被多次调用 - 堆栈内存溢出...

我正在尝试计算每次点击的下载次数,但是问题是,当一次多次调用服务时,我点击如下所示:

new_downloads: 15

127.0.0.1 - - [28/Jun/2019 17:30:46] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 200 -

new_downloads: 16

new_downloads: 17

127.0.0.1 - - [28/Jun/2019 17:30:46] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 200 -

127.0.0.1 - - [28/Jun/2019 17:30:47] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -

new_downloads: 18

new_downloads: 18

new_downloads: 18

new_downloads: 18

127.0.0.1 - - [28/Jun/2019 17:30:48] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -

127.0.0.1 - - [28/Jun/2019 17:30:48] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -

127.0.0.1 - - [28/Jun/2019 17:30:48] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -

127.0.0.1 - - [28/Jun/2019 17:30:48] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -

这是我在烧瓶中的功能:

@app.route('/document/down')

def down_document():

executed = False

document_id = request.args.get('document_id')

document_title = request.args.get('document_title')

directory = os.path.dirname(app.instance_path) + "\\corpus" + "\\" + document_id

# resp = make_response(send_file(path, as_attachment=True))

resp = make_response(send_from_directory(directory=directory, filename=document_title))

if resp:

executed = True

if executed:

old_downloads = Helper.get_num_downloads(document_id)

new_downloads = old_downloads + 1

print("new_downloads: " + str(new_downloads))

Helper.refresh_downloads(document_id=document_id, new_downloads=new_downloads)

if request.args.get('username'):

Helper.add_relationship_user_download_doc(username=request.args.get('username'), document_id=document_id)

resp.headers['Access-Control-Allow-Origin'] = '*'

resp.headers['Content-Type'] = 'application/force-download'

resp.headers['Content-type'] = 'application/pdf'

resp.headers['Content-Description'] = 'File Download'

resp.headers['Content-Disposition'] = 'attachment'

resp.headers['Content-Transfer-Encoding'] = 'binary'

resp.headers['Expires'] = 'must-revalidate, post-check=0, pre-check=0'

resp.headers['Pragma'] = 'public'

return resp

当我删除函数send_from_directory时,api会被调用一次,但我添加一个api会被调用多次。

这是我的ajax

$.ajax({

url: "http://127.0.0.1:5000/document/down",

type: "GET",

crossDomain: true,

dataType: 'binary',

data: {

"document_id": document_id,

"document_title": filename,

},

success: function (response) {

console.log("success download");

Swal.close();

},

failure: function (request) {

alert(request);

},

error: function (jqXHR, exception) {

console.log("error download");

let msg = '';

if (jqXHR.status === 0) {

msg = 'Not connect.\n Verify Network.';

} else if (jqXHR.status === 404) {

msg = 'Requested page not found. [404]';

} else if (jqXHR.status === 500) {

msg = 'Internal Server Error [500].';

} else if (exception === 'parsererror') {

msg = 'Requested JSON parse failed.';

} else if (exception === 'timeout') {

msg = 'Time out error.';

} else if (exception === 'abort') {

msg = 'Ajax request aborted.';

} else {

msg = 'Uncaught Error.\n' + jqXHR.responseText;

}

alert(msg)

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值