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.

Question: How to async with two independent Aff/TryAsync

See original GitHub issue

Hi,

I have a little question about the best way handling two different async Monads with side effects, like Aff and TryAsync. The best thing about async is that it frees the thread for work while an asynchronous operation is waiting for a result of an external source. So sometimes, when two tasks are indepentend of each other, it allows us to run them kind of parallel. In the example below we have to start two different http requests which do not need the result of the other, so we can start the second before the first one has finished

var request1Task = Request1();
var request2Task = Request2();

await request1Task;
await request2Task;

But things get tricky (for me) when it comes to the effect monads. In the following example I will return Aff instead of raw Tasks, but the only way I found to run the execution like the one above is with calling Run on both and awaiting the Fin.

var request1Running = Request1().Run();
var request2Running = Request2().Run();

var fin1 = await request1Running;
var fin2 = await request2Running;

However, from my understanding of the docs the effects should be composed and only be run once at the start of the application, which is broken by this code style. I understand that this code is not executed directly when wrapped in another Aff itself. That way the rule can be followed but I don’t know if that’s how it’s supposed to be. So am I missing another way to achieve the effect of the code in the example above?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
louthycommented, May 31, 2022

With Aff you can call:

  Aff<(R1, R2)> mx = (Request1(), Request2()).Sequence();

With up to 5 items in the tuple (which will all run in parallel, and only return when all are complete).

TryAsync doesn’t have this behaviour. I’d encourage you to use Aff as it’s going to get the lion share of the effort going forward.

1reaction
LaszloLueckcommented, Jun 10, 2022

What more important IMHO is e.g. if you have an Enumerable<Task<Sideeffects (external API-Call)>>. If you take e.g. an Enumerable of 1.000.000 of those Tasks and do a .Select or .ForEach, there is a big chance that you crash the external API or the network or even both. With TraverseParallel with the window of parallelity you can backpressure the amount of simultaneous call to external.

(It´s like groupBy(count) and foldLeft in scala, if you don´t want to use fs2 or akka.streams).

Btw. Thanks for that outstanding framework!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle an async function that depends on multiple ...
I have an asynchronous function that works with the result of two other async functions. ... Is there any other way to handle...
Read more >
How to run two async functions forever - Python
Running two async functions forever Python:​​ Method 1: Just use the while True loop in the main function: Python3.
Read more >
Understanding Control Flow with Async and Await in C# | ...
In the previous guide in this series we took a look at the basics of the async and await keywords in C#. Once...
Read more >
Getting Started With Async Features in Python
This is a mechanism that allows multiple sections of one program to run at the same time. Each section of code that runs...
Read more >
JavaScript Async/Await Tutorial – Learn Callbacks, ...
Synchronous system, three images are in the same lane. One can't overtake the other. The race is finished one by one. If image...
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