1

I have the following:

do!
    [
        [
            TradesDatabase.deleteTradesAsync    credentials time (time.Add oneDay)
            TradesDatabase.insertTradesAsync    credentials trades
        ] |> Async.Sequential
        
        [
            KVWAPDatabase.deleteKVWAPAsync      credentials time (time.Add oneDay)
            KVWAPDatabase.insertKVWAPAsync      credentials output.KVWAP
        ] |> Async.Sequential
        
        [
            CandlesDatabase.deleteCandlesAsync  credentials time (time.Add oneDay)
            CandlesDatabase.insertCandlesAsync  credentials output.Candles
        ] |> Async.Sequential
    ]
    |> Async.Parallel
    |> Async.Ignore

The inner code is probably not very useful, these are async db calls.

So for 3 data types I want to process in parallel, I want to do a sequential delete following by an insert.

But the debug's printout is:

delete kvwap              <- called by deleteKVWAPAsync
inserting kvwap           <- called by insertKVWAPAsync
done deleting kvwap       <- called by deleteKVWAPAsync

so this block:

[
    KVWAPDatabase.deleteKVWAPAsync      credentials time (time.Add oneDay)
    KVWAPDatabase.insertKVWAPAsync      credentials output.KVWAP
] |> Async.Sequential

was supposed to be executed in order, but it's not.

What am I missing regarding Async.Sequential?

8
  • I'm not able to reproduce this, even if I put a long Async.Sleep in the delete step. I suspect that maybe you've accidentally copy/pasted the kvwap debug output into one of the other parallel branches (either Trades or Candles) without changing the output text? Commented Apr 18, 2022 at 17:45
  • It's separate pieces of code and the DB complains about it as well, so the delete is not done in time. When I do the deletes with let!/and!/and! and then the inserts with let!/and!/and! it works properly though. Thanks for looking into it, I'll keep investigating! Commented Apr 18, 2022 at 17:56
  • Why don't you use an async CE and FusionTasks NuGet and call your async code inside the CE? Commented Apr 27, 2022 at 11:38
  • @Andrii if something can run with the base libs, it's better than adding anther package Commented Apr 27, 2022 at 12:09
  • If something is already implemented is better to reuse it than invent the wheel 😉 Commented Apr 28, 2022 at 18:42

1 Answer 1

2

I have experienced a similar issue.

It may be the case that your database functions are being run as "hot" tasks. For example, if I remember correctly the following code starts the computation immediately, even though it is returning an Async<int>:

  type DbContext =
   Microsoft.Data.SqlClient.SqlConnection * Microsoft.Data.SqlClient.SqlTransaction

  let doQuery ((connection, transaction) : DbContext) query =
      (connection.ExecuteAsync(query, transaction))
      |> Async.AwaitTask

While this code does not:

  let doQuery2 ((connection, transaction) : DbContext) query =
    async {
      return! 
        (connection.ExecuteAsync(query, transaction))
        |> Async.AwaitTask
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.