Q: Is `Execute` Async or Not ?
See original GitHub issueThe docs say that Execute
is synchronous but it is awaitable asks for a Task as a parameter.
Sorry if this sounds dumb due to my C# async experience but I am a bit confused. For example: does this make sense ?
policy.Execute(() => Task.Run(() => MyTask()));
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Node.js Asynchronous Library Comparison - Q vs Async
Both libraries are good. I have discovered that they serve separate purposes and can be used in tandem. Q provides the developer with ......
Read more >Use ExecuteAsync to execute messages asynchronously
Execute. ExecuteAsyncResponse returns with the ID of the asynchronous job. You can (optionally) query the job using the ID to find out its ......
Read more >Execute async on self? - KX Community - 13321
Hi,. I have a decision tree function and rather than run it recursively and building a stack I'd like to trigger the next...
Read more >Async IO in Python: A Complete Walkthrough
This tutorial will give you a firm grasp of Python's approach to async IO, which is a concurrent programming design that has received...
Read more >How To Do @Async in Spring
In this tutorial, we'll explore the asynchronous execution support in Spring and the @Async annotation. Simply put, annotating a method of a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To comment on:
policy.Execute(() => Task.Run(() => MyTask()));
: it doesn’t make sense.Task.Run(() => MyTask()
on its own (disregarding Polly for a moment) creates and immediately returns aTask
representingMyTask()
running.The example doesn’t capture that
Task
into any variable to observe later, nor synchronously wait on its outcome (eg.Wait()
,.Result
), nor asynchronouslyawait
it. It thus creates an orphaned task which risks an UnobservedTaskException ifMyTask()
throws.policy.Execute(...)
is synchronous, but it only governsTask.Run(...)
near-immediately returning theTask
representingMyTask()
running. It won’t capture any exceptions thrown byMyTask()
, nor (if that was desired) wait synchronously or asynchronously forMyTask()
to complete.Hope this helps!
Glad @cemremengu if it’s useful, and thanks for the doco idea! Since the async gotchas we’ve discussed are async general rather than specific to Polly, and already decently covered in the blogosphere/Stack Overflow, think I’ll keep the Polly doco purely Polly-focused. But: thanks for the idea!
If you’re looking for some good references on
Task
andasync
/await
, some great starting points are:Cheers