Open sherlockwu opened 3 years ago
how should I catch the exception for upload_text_async() ? simply replace upload_text by upload_text_async cannot catch the exception.
auto async_task = blob4.upload_text_async(U("test new overwrite text"), condition4, options4, context4);
try {
async_task.get(); // block until the task is finished.
}
catch (const std::exception& e) {
std::cout << U("Error: ") << e.what() << std::endl;
}
Thanks for your reply!
Uhm, would that piece of code become sync upload then?
is there a way I can get the return "message" of async or sync operations, instead of by throwing exceptions?
The return type for upload_text
and unpload_text_async
is void, so there isn't any "message".
Take cloud_blob::exists() for example, the sync version returns a bool
. So if the blob actually exists, that function returns true
. If the blob doesn't exists, the function returns false
. If any error happens (like authentication error), the function will throw.
For async version, calling .get()
on the returned task will return true
or false
or throw.
Uhm, would that piece of code become sync upload then?
In your sample code, yes. Because there's only one task. You can start a bunch of asynchronous tasks and then wait for them in one thread, like
std::vector<pplx::task<T>> tasks;
tasks.emplace_back(an_async_function());
for (auto& t: tasks) {
t.get();
}
Note that it's your responsibility to observe every potential exception in async task. So if you're pretty sure some async task won't throw, you don't have to call .get()
.
@sherlockwu BTW, this project has been deprecated in favor of our new version of storage SDK. So if you're starting a new project, you may want to try the new sdk.
Thanks for your reply.
Perhaps I could talk more about our expected behaviors.
We'd like to
Now, I'm using .then() to implement the follow-up works. However, turns out we cannot know the uploads succeeded or not. What would you suggest us to implement with the APIs? thanks
Thanks for telling us about storage SDK. Would our need be easier to implement with azure-sdk? Thanks
@sherlockwu
Every API has an overload which takes an access_condition
as parameter, where you can set if-not-exist condition, so that the operation will fail and throw exception if the blob already exists. (Which I see you already did in your sample code)
In the .then()
continuation task, you can tell if the last operation succeeded or not by checking if there's an exception. Like
auto t = blob4.upload_text_async(U("test new overwrite text"), condition4, options4, context4).then([](pplx::task<void> previous_task) {
try {
previous_task.get();
}
catch (azure::storage::storage_exception& e)
{
// failed, do clean up work here
}
// not failed, do other work
});
t.wait(); // This waits for the continuation task (defined by the lambda).
Would our need be easier to implement with azure-sdk?
The new SDK only supports synchronous API, which is much easier to use.
Asynchronous task in this sdk is less easy to comprehend and is error-prune. In addition, although the API interface is asynchronous, it uses a thread-pool under the hood, so there's really no performance gain here. Actually our new SDK has much better performance than this old sdk.
Got you.
That sample code looks like what we indeed need. Let me have a try and get back to you.
Thanks a lot for the help.
Hi all,
I have a question related to catching exception for azure blob async upload operations. Here is my sync version: (upload if not exist)
The catch part will catch the exception if the blob already exists.
My question is:
Thank you, Kan