同步curl请求 [英] Synchronized curl requests

查看:110
本文介绍了同步curl请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试对多个目标执行HTTP请求,我需要他们在几乎同时运行。

I'm trying to do HTTP requests to multiple targets, and I need to them to run (almost) exactly at the same moment.

我试图为每个请求创建一个线程,但我不知道为什么Curl在执行perform时崩溃。我使用一个简单的处理每个线程,所以在理论上一切都应该确定...

I'm trying to create a thread for each request, but I don't know why Curl is crashing when doing the perform. I'm using an easy-handle per thread so in theory everything should be ok...

有人有类似的问题吗?或者有人知道多界面是否允许您选择何时执行所有请求?

Has anybody had a similar problem? or Does anyone know if the multi interface allows you to choose when to perform all the requests?

非常感谢。

编辑:

以下是代码示例:

void Clazz::function(std::vector<std::string> urls, const std::string& data)
{
    for (auto it : urls)
    {
        std::thread thread(&Clazz::DoRequest, this, it, data);
        thread->detach();
    }
}

int Clazz::DoRequest(const std::string& url, const std::string& data)
{
    CURL* curl = curl_easy_init();
    curl_slist *headers = NULL;

    headers = curl_slist_append(headers, "Expect:"); 
    headers = curl_slist_append(headers, "Content-Type: application/json");

    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());

    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1L);

    //curlMutex.lock();
    curl_easy_perform(curl);
    //curlMutex.unlock();
    long responseCode = 404;
    curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &responseCode);

    curl_easy_cleanup(curl);
    curl_slist_free_all(headers);
}

我希望这可以帮助,谢谢!

I hope this can help, thanks!

在设备中正确调试后,发现问题是一个老的知道问题与curl。

http://curl.haxx.se/mail/lib-2010-11/0181.html

http://curl.haxx.se/mail/在每个curl句柄中使用CURLOPT_NOSIGNAL之后,崩溃已经消失了。

:)

after using CURLOPT_NOSIGNAL in every curl handle the crash has disappeared. :)

这篇关于同步curl请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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