Python flask and AngularJS with Chaining http post Requests



我正在尝试将数据拆分为块,然后通过AngularJS$http.post调用将其发送到Python flask。在JS方面,请求似乎是按给定的顺序发出的。但是在Python方面,请求似乎没有按照预期的顺序到达。

临时代码如下所示:

.JS:

var xdata = ["0", "1", "2", "3", "4", "***__END__***"].map(x => JSON.stringify(x));
var post_chain = [];
for (var i = 0; i < xdata.length; i++) {
post_chain.push($http.post("/path", xdata[i]));
}
post_chain[0].then(function (response0) {
console.log("response0");
return post_chain[1];
}).then(function (response1) {
console.log("response1");
return post_chain[2];
}).then(function (response2) {
console.log("response2");
return post_chain[3];
}).then(function (response3) {
console.log("response3");
return post_chain[4];
}).then(function (response4) {
console.log("response4");
return post_chain[5];
}).then(function (response) {
console.log('Done');
// Handle response
});

在 Python 中,我使用flask.request.get_json(silent=False)来获取输入数据。有一些代码可以检测终止标准(例如,由字符串"***__END__***"给出)表示来自 JS 的帖子结束。这就是为什么我需要确保从JS到Python的请求顺序。我还打印出接收到的数据。

JS中的控制台输出看起来不错(它按给定顺序显示0,1,2等)。但是在 Python 中,打印的数据是无序的(0、4、1 等)。

稍后,我打算使用递归函数来概括JS代码,例如。但是现在,我试图保证Python代码以正确的顺序接收数据。

有什么建议吗?

更新 1

建议有效!但是,理想的方法是概括任何数组大小的代码。我认为这可以通过递归来完成,但它似乎没有以正确的顺序传递数据。缺少什么?

var post_chain_call = function(i, post_element) {
if (i == post_chain.length - 1) {
return post_element(xdata[i]).then(function (response) {
// Handle response
});
} else {
return post_element(xdata[i]).then(post_chain_call(i + 1, post_chain[i + 1]));
}
}
post_chain_call(0, post_chain[0]);

更新 2

让它与另一种方法一起工作!

var post_chain_call = function(i, post_element) {
if (i == post_chain.length - 1) {
return post_element.then(function (response) {
// Handle response
});
} else {
return post_element.then(function (response_tmp) {
return post_chain_call(i + 1, post_chain[i + 1](xdata[i + 1]));
});
}
}
post_chain_call(0, post_chain[0](xdata[0]));

前端开始与后端通信的那一刻就是您将它们分配给postChain的时候。这意味着每个请求本身都在尝试与服务器通信,并且无法预测它们连接到服务器的顺序。确保请求仅在上一个请求的.then()实例化的可能解决方法是不将$http.post()调用的结果存储在链中,而是将执行$http.post()并返回该承诺的函数存储。

var xdata = ["0", "1", "2", "3", "4", "***__END__***"].map(x => JSON.stringify(x));
var post_chain = [];
for (var i = 0; i < xdata.length; i++) {
// this way, the code is not yet executed, but it will when you call it.
post_chain.push((data) => $http.post("/path", data));
}
post_chain[0](xdata[0]).then(function (response0) {
console.log("response0");
return post_chain[1](xdata[1]);
}).then(function (response1) {
console.log("response1");
return post_chain[2](xdata[2]);
}).then(function (response2) {
console.log("response2");
return post_chain[3](xdata[3]);
}).then(function (response3) {
console.log("response3");
return post_chain[4](xdata[4]);
}).then(function (response4) {
console.log("response4");
return post_chain[5](xdata[5]);
}).then(function (response) {
console.log('Done');
// Handle response
});

您还可以使用箭头函数来确保xdata仍在函数范围内,并且不需要向其传递任何参数

var post_chain = [];
for (var i = 0; i < xdata.length; i++) {
// this way, the code is not yet executed, but it will when you call it.
post_chain.push(() => $http.post("/path", xdata[i]));
}
post_chain[0]().then((response0) => {
console.log("response0");
return post_chain[1]();
}).then((response1) => {
console.log("response1");
return post_chain[2]();
}).then((response2) => {
console.log("response2");
return post_chain[3]();
}).then((response3) => {
console.log("response3");
return post_chain[4]();
}).then((response4) => {
console.log("response4");
return post_chain[5]();
}).then((response) => {
console.log('Done');
// Handle response
});

我希望这有所帮助。

最新更新