0
我查看了許多問題和答案,但沒有一個適合我。 有沒有辦法在Python中實現類似AJAX的功能?類似AJAX的非阻塞異步Python請求
比方說,你有這樣的設置:
url = "http://httpbin.org/delay/5"
print(requests.get(url))
foo()
由於代碼執行requests.get
塊,foo()
不會觸發,直到你有一個服務器響應。
在Javascript中,例如,該腳本將繼續工作:
var requests = {
get: function(url, callback) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
callback(this);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
}
function response_goes_through_here(r) {
console.log(r.responseText);
}
var url = "http://httpbin.org/delay/5"
requests.get(url, response_goes_through_here)
foo()
我試過grequests
,但它仍然掛起,直到整個隊列完成。
它是否阻塞了線程?在收到響應之前,我看到大多數示例停止代碼執行。 –