Swift-log API add async await to avoid problems in async code

Thanks for your reply. I maybe clarify a bit as the problem relates to another issue about ordering of tasks as mentions in other thread Task: Is order of task execution deterministic?

My issue was that the use of Dispatchqueue.global.async { } was hidden inside the log function.This made the logs as you describe offload the work to queue but puppy lacs to my understanding the await puppy.gracefulShutdown() which seams to me to be the underlying issue.

As for my question to have an async function to log. This was me looking for a way to express, like you describe, to send but don't wait in the current context. As I understand now this does not seam possible with the current async await.

What I like about async await is that it lets you express that there is async work going on in a function and that this cannot be hidden.

So the way I end up using the logs in my code is using async let but this becomes very verbose.

#!/usr/bin/env swift
import Foundation

let mainTask = Task {
    let work = Task {
      // do some work
      await logger.logAsync("Log some small amount")
     // continue
    }
    async let workResult: () = work.value
    async let heavyLog: () = log.logAsync("large log ...")

    await _ = [workResult, heavyLog]
}

await  mainTask.value
await logger.logAsync("all work including async logs done")

So what I'm missing is indeed the await puppy.gracefulShutdown() and a way for a function to express that it is doing work that will finish without waiting.

func log() parallel

So unlike await you would need to call this function with queue

queue log()

Queue would be a special global or something that you could wait for.

\\ do some other work in main
await queue.finished

Which could or could not return a value.

I hope this clarifies my need? I just expressed my first naive intension with async/await. No clue if it is something that is logical to others too.
But thanks very much for both of your clear explanation of the problem. I think I can find a workaround now for puppy.

1 Like