mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-05 20:47:53 +00:00
Use addCallback rather than callback= in asyncfutures.all() (#6850)
* Use addCallback rather than callback= in asyncfutures.all() Addresses part of #6849 * Stop using do notation for #6849 * Update example style
This commit is contained in:
committed by
Dominik Picheta
parent
f70d967d2c
commit
b87ef65532
@@ -59,9 +59,10 @@ export asyncfutures, asyncstreams
|
||||
##
|
||||
## .. code-block::nim
|
||||
## var future = socket.recv(100)
|
||||
## future.callback =
|
||||
## future.addCallback(
|
||||
## proc () =
|
||||
## echo(future.read)
|
||||
## )
|
||||
##
|
||||
## All asynchronous functions returning a ``Future`` will not block. They
|
||||
## will not however return immediately. An asynchronous function will have
|
||||
@@ -1611,4 +1612,4 @@ proc waitFor*[T](fut: Future[T]): T =
|
||||
|
||||
fut.read
|
||||
|
||||
{.deprecated: [setEvent: trigger].}
|
||||
{.deprecated: [setEvent: trigger].}
|
||||
|
||||
@@ -333,7 +333,7 @@ proc all*[T](futs: varargs[Future[T]]): auto =
|
||||
let totalFutures = len(futs)
|
||||
|
||||
for fut in futs:
|
||||
fut.callback = proc(f: Future[T]) =
|
||||
fut.addCallback proc (f: Future[T]) =
|
||||
inc(completedFutures)
|
||||
if not retFuture.finished:
|
||||
if f.failed:
|
||||
@@ -355,7 +355,7 @@ proc all*[T](futs: varargs[Future[T]]): auto =
|
||||
|
||||
for i, fut in futs:
|
||||
proc setCallback(i: int) =
|
||||
fut.callback = proc(f: Future[T]) =
|
||||
fut.addCallback proc (f: Future[T]) =
|
||||
inc(completedFutures)
|
||||
if not retFuture.finished:
|
||||
if f.failed:
|
||||
|
||||
@@ -40,6 +40,16 @@ proc testVarargs(x, y, z: int): seq[int] =
|
||||
|
||||
result = waitFor all(a, b, c)
|
||||
|
||||
proc testWithDupes() =
|
||||
var
|
||||
tasks = newSeq[Future[void]](taskCount)
|
||||
fut = futureWithoutValue()
|
||||
|
||||
for i in 0..<taskCount:
|
||||
tasks[i] = fut
|
||||
|
||||
waitFor all(tasks)
|
||||
|
||||
block:
|
||||
let
|
||||
startTime = cpuTime()
|
||||
@@ -57,6 +67,13 @@ block:
|
||||
|
||||
doAssert execTime * 1000 < taskCount * sleepDuration
|
||||
|
||||
block:
|
||||
let startTime = cpuTime()
|
||||
testWithDupes()
|
||||
let execTime = cpuTime() - startTime
|
||||
|
||||
doAssert execTime * 1000 < taskCount * sleepDuration
|
||||
|
||||
block:
|
||||
let
|
||||
startTime = cpuTime()
|
||||
|
||||
Reference in New Issue
Block a user