問題描述
我開始使用 curl
同步處理 http 請求.我的問題是如何異步執行此操作?
我做了一些搜索,從這個curl_multi_*接口的文檔-synchronous-or-asynchronous">問題 和這個 示例 但它根本沒有解決任何問題.
我的簡化代碼:
回調方法 writeCallback
沒有被調用,什么也沒有發生.
請多多指教.
根據@Remy 的以下回答,我得到了這個,但似乎并不是我真正需要的.因為使用循環仍然是一個阻塞循環.如果我做錯或誤解了什么,請告訴我.實際上,我對 C++ 還很陌生.
這是我的代碼:
我現在可以同時執行 2 個 http 請求.回調被調用但仍需要在執行以下行之前完成.我需要考慮線程嗎?
再次仔細閱讀文檔,尤其是以下部分:
http://curl.haxx.se/libcurl/c/libcurl-multi.html
<塊引用>你的應用程序可以從 libcurl 獲取知識,當它想要被調用來傳輸數據時,這樣你就不必忙循環并調用它 curl_multi_perform(3) 像瘋了一樣.curl_multi_fdset(3) 提供了一個界面,您可以使用它從 libcurl 中提取 fd_sets 以在選擇中使用() 或 poll() 調用以了解何時可能需要注意多堆棧中的傳輸.這也使您的程序可以很容易地同時等待對您自己的私有文件描述符的輸入,或者可能時不時地超時(如果您愿意的話).
http://curl.haxx.se/libcurl/c/curl_multi_perform.html><塊引用>
當應用程序發現有數據可用于 multi_handle 或超時已過時,應用程序應調用此函數來讀取/寫入現在要讀取或寫入的任何內容等. curl_multi_perform() 在讀/寫完成后立即返回.該函數不需要實際有任何數據可供讀取或可以寫入數據,可以調用它以防萬一.它將在第二個參數的整數指針中寫入仍在傳輸數據的句柄數.
如果 running_handles 的數量與上一次調用相比發生了變化(或者小于您添加到多句柄的簡單句柄數量),則您知道存在一次或多次轉移少跑".然后,您可以調用 curl_multi_info_read(3) 以獲取有關每個已完成傳輸的信息,返回的信息包括CURLcode 等.如果添加的句柄很快失敗,它可能永遠不會算作 running_handle.
當該函數返回時 running_handles 設置為零 (0) 時,不再有任何傳輸正在進行.
換句話說,你需要運行一個循環來輪詢 libcurl 的狀態,每當有數據等待傳輸時調用 curl_multi_perform()
,根據需要重復,直到沒有任何數據可以傳輸.
您鏈接到的博客文章提到了這種循環:
<塊引用>代碼可以這樣使用
http http;
http:AddRequest("http://www.google.com");
//在一些稱為每一幀的更新循環中
http:Update();
您沒有在代碼中進行任何循環,這就是沒有調用回調的原因.當您調用 curl_multi_perform()
一次時,尚未收到新數據.
I have come to use curl
synchronously doing a http request. My question is how can I do it asynchronously?
I did some searches which lead me to the documentation of curl_multi_*
interface from this question and this example but it didn't solve anything at all.
My simplified code:
The callback method writeCallback
doesn't get called and nothing happens.
Please advise me.
EDIT:
According to @Remy's below answer, I got this but seems that it's not quite what I really needed. Cause using a loop is still a blocking one. Please tell me if I'm doing wrong or misunderstanding something. I'm actually pretty new to C++.
Here's my code again:
I can now do 2 http requests simultaneously. Callbacks are called but still need to finish before executing following lines. Will I have to think of thread?
Read the documentation again more carefully, particularly these portions:
http://curl.haxx.se/libcurl/c/libcurl-multi.html
Your application can acquire knowledge from libcurl when it would like to get invoked to transfer data, so that you don't have to busy-loop and call that curl_multi_perform(3) like crazy. curl_multi_fdset(3) offers an interface using which you can extract fd_sets from libcurl to use in select() or poll() calls in order to get to know when the transfers in the multi stack might need attention. This also makes it very easy for your program to wait for input on your own private file descriptors at the same time or perhaps timeout every now and then, should you want that.
http://curl.haxx.se/libcurl/c/curl_multi_perform.html
When an application has found out there's data available for the multi_handle or a timeout has elapsed, the application should call this function to read/write whatever there is to read or write right now etc. curl_multi_perform() returns as soon as the reads/writes are done. This function does not require that there actually is any data available for reading or that data can be written, it can be called just in case. It will write the number of handles that still transfer data in the second argument's integer-pointer.
If the amount of running_handles is changed from the previous call (or is less than the amount of easy handles you've added to the multi handle), you know that there is one or more transfers less "running". You can then call curl_multi_info_read(3) to get information about each individual completed transfer, and that returned info includes CURLcode and more. If an added handle fails very quickly, it may never be counted as a running_handle.
When running_handles is set to zero (0) on the return of this function, there is no longer any transfers in progress.
In other words, you need to run a loop that polls libcurl for its status, calling curl_multi_perform()
whenever there is data waiting to be transferred, repeating as needed until there is nothing left to transfer.
The blog article you linked to mentions this looping:
The code can be used like
Http http;
http:AddRequest("http://www.google.com");// In some update loop called each frame
http:Update();
You are not doing any looping in your code, that is why your callback is not being called. New data has not been received yet when you call curl_multi_perform()
one time.
這篇關于如何在 C++ 中異步執行 curl_multi_perform()?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!