Expert help me see, how to run the results of asynchronous?
import asyncdispatch
proc longtime() {.async.}=
var x=0
for i in 1..5000000:
inc(x)
echo x
proc asyncproc() {.async.}=
echo "async start"
await longtime()
echo "async end"
proc main()=
echo "main start"
discard asyncproc()
echo "main end"
main()
Operation result:
main start
async start
5000000
async end
main end
[Finished in 2.1s]
How to make the result of the operation is asynchronous:
main start
async start
main end
5000000
async end
[Finished in 2.1s]
Something like this will do what you want:
import asyncdispatch
var asyncProcFut: Future[void]
proc longtime() {.async.} =
await sleepAsync(2000)
proc asyncproc() {.async.} =
echo "async start"
await longtime()
echo "async end"
proc main()=
echo "main start"
asyncProcFut = asyncproc()
echo "main end"
main()
waitFor asyncProcFut
You have to understand that async is for IO-bound operations, not CPU-bound ones. It only runs in a single thread.