question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Q: Is `Execute` Async or Not ?

See original GitHub issue

The 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:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
reisenbergercommented, Dec 29, 2016

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 a Task representing MyTask() running.

  • The example doesn’t capture that Task into any variable to observe later, nor synchronously wait on its outcome (eg .Wait(), .Result), nor asynchronously await it. It thus creates an orphaned task which risks an UnobservedTaskException if MyTask() throws.

  • policy.Execute(...) is synchronous, but it only governs Task.Run(...) near-immediately returning the Task representing MyTask() running. It won’t capture any exceptions thrown by MyTask(), nor (if that was desired) wait synchronously or asynchronously for MyTask() to complete.

Hope this helps!

1reaction
reisenbergercommented, Dec 30, 2016

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 and async/await, some great starting points are:

Cheers

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found