怎么办curl_multi_perform()在异步C ++? [英] How to do curl_multi_perform() asynchronously in C++?

查看:1535
本文介绍了怎么办curl_multi_perform()在异步C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来使用卷曲同步做一个HTTP请求。我的问题是我怎么能做到这一点异步?

I have come to use curl synchronously doing a http request. My question is how can I do it asynchronously?

我做了一些搜索这导致我 curl_multi _ * 界面从这个<一个文档href=\"http://stackoverflow.com/questions/15311667/is-curl-easy-perform-synchronous-or-asynchronous\">question这例如,但它并没有解决任何事情。

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.

我的简化code:

CURLM *curlm;
int handle_count = 0;
curlm = curl_multi_init();

CURL *curl = NULL;
curl = curl_easy_init();

if(curl)
{
    curl_easy_setopt(curl, CURLOPT_URL, "http://stackoverflow.com/");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
    curl_multi_add_handle(curlm, curl);
    curl_multi_perform(curlm, &handle_count);
}

curl_global_cleanup();

回调方法 writeCallback 不会被调用并没有任何反应。

The callback method writeCallback doesn't get called and nothing happens.

请告诉我。

编辑:

据@雷米的回答以下,我得到了这一点,但似乎这不是完全是我真正需要的。因为使用循环仍然是一个阻断一个。请告诉我,如果我做错了或误解的东西。实际上,我pretty新的C ++。

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++.

下面是我的code又说:

Here's my code again:

int main(int argc, const char * argv[])
{
    using namespace std;
    CURLM *curlm;
    int handle_count;
    curlm = curl_multi_init();

    CURL *curl1 = NULL;
    curl1 = curl_easy_init();

    CURL *curl2 = NULL;
    curl2 = curl_easy_init();

    if(curl1 && curl2)
    {
        curl_easy_setopt(curl1, CURLOPT_URL, "http://stackoverflow.com/");
        curl_easy_setopt(curl1, CURLOPT_WRITEFUNCTION, writeCallback);
        curl_multi_add_handle(curlm, curl1);

        curl_easy_setopt(curl2, CURLOPT_URL, "http://google.com/");
        curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, writeCallback);
        curl_multi_add_handle(curlm, curl2);

        CURLMcode code;
        while(1)
        {
            code = curl_multi_perform(curlm, &handle_count);

            if(handle_count == 0)
            {
                break;
            }
        }
    }

    curl_global_cleanup();

    cout << "Hello, World!\n";
    return 0;
}

我现在能做的同时向2个HTTP请求。回调被称为但仍需要执行以下行之前完成。我将不得不考虑线程?

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

您的应用程序可以从获取知识的libcurl时,它想被调用来传输数据,这样就不必忙着环和调用的 curl_multi_perform(3)像疯了似的。 curl_multi_fdset(3)提供了使用它你可以从提取此类变量的接口在libcurl的选择()或poll使用()为了呼吁结识时,在多堆栈中的转移可能需要注意。这也使得它很容易为​​你的程序等待你自己的私人文件描述符输入在同一时间或者超时现在每然后,你应该希望如此。

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

当一个应用程序已经发现了有可用于multi_handle或超时数据已过,应用程序应该调用这个函数来读/写任何有读或现在等curl_multi_perform写()返回只要读/写操作就完成了。此功能不需要有实际上是可供读取的任何数据或数据可以被写入,也可以以防万一被调用。它会写,在第二个参数的整数指针仍然传输数据处理器的数量。

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.

如果量的 running_handles 的从previous来电更改(或小于手柄您添加的多柄易金额),你知道,有一个以上转让少跑。然后,您可以调用 curl_multi_info_read(3)获取有关每一个人完成的传输信息,而返回的信息包括卷曲code等等。如果添加手柄很快失败,它可能永远不会被算作running_handle。

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.

running_handles 的设置为零(0)这个函数的返回,不再有任何正在进行中的传输。

When running_handles is set to zero (0) on the return of this function, there is no longer any transfers in progress.

在换句话说,你需要运行一个循环,民意调查的libcurl其身份,称 curl_multi_perform(),每当有数据等待被转移,因为直到需要重复没有什么留下来传输。

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:

在code可用于像

的Http HTTP;

  HTTP:addRequest设置( http://www.google.com );

Http http;
http:AddRequest("http://www.google.com");

//在一些所谓的每一帧
更新循环
  HTTP:更新();

您没有做任何的循环在code,这就是为什么你的回调不会被调用。新数据尚未收到,当你调用 curl_multi_perform()一次。

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.

这篇关于怎么办curl_multi_perform()在异步C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆