libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.49k stars 924 forks source link

multiple calls to PostAsync #909

Closed ronytigo closed 1 year ago

ronytigo commented 1 year ago

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?

COM8 commented 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.

ronytigo commented 1 year ago

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?

ronytigo commented 1 year ago

and when specifically will the Body of the Post request will be freed from memory?

COM8 commented 1 year ago

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.
ronytigo commented 1 year ago

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!

ronytigo commented 1 year ago

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.

COM8 commented 1 year ago

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.

ronytigo commented 1 year ago

no, every 1 millisecond. I'll try that direction, thanks!

COM8 commented 1 year ago

Closing this one for now. Feel free to reopen if necessary.