Closed ronytigo closed 1 year ago
This is not possible. CPR will always retrieve the response from curl. Since curl provides the response always anyway converting it to a cpr response is negligible compared to the time and effort required for performing a request.
so when will the resources be freed? If I just call PostAsync and don't save the Future that it returns and therefore don't do anything with the Future, what would happen?
and when specifically will the Body of the Post request will be freed from memory?
There exist two copies of your body inside cpr. One managed by curl and one by cpr.
If you do not use the cpr::Session
obj then the curl one gets cleaned up after returning the response. Else, it will live as long as the cpr::Session
obj lives or until you make a new request e.g. overriding it.
If you do not store the returned cpr::AsyncResponse
anywhere, it will be cancelled when the destructor is called.
I suggest you do something like:
1. Create a bunch of requests and store the `cpr::AsyncResponse` objects inside a `std::array` or `std::vector`.
2. Once the first bunch is done, call `.wait()` on every one of them to ensure all are done.
3. Clear the array/vector and continue with step 1.
I hold 10 InfluxDbClient objects. Each one holds a cpr Session. I read data from plc every 10 seconds for each InfluxDbClient, so the system gets data around every 1ms. I want to write the data to influx without waiting for the response. I want to keep getting data from the plc and send it to influx. So in many cases I want to send many requests before any of them returned. What would be the best approach?
"it will be cancelled when the destructor is called." - when is it called? Thanks!
And what does it mean to be cancelled? If the call was made and is handled by the influx server and it's cancelled in the cpr client side and doesn't affect the influxdb server side, that sounds good.
gets data around every 1ms.
Do you mean every 1s here?
I still suggest using this approach: https://github.com/libcpr/cpr/issues/909#issuecomment-1537294543
If you do not store the returned async response, the destructor will be called immediately and your response will be cancelled.
no, every 1 millisecond. I'll try that direction, thanks!
Closing this one for now. Feel free to reopen if necessary.
Is there a way to call PostAsync a lot of times and ignore the response? I can ignore the response from PostAsync but I don't want the library to get the response from each PostAsync, keep the http connection open until it's finished etc. I want to make the Post request and clean any resources immediately, is that possible?