From 03f4a26829f4b789bc2a031db93fa7eb48517b50 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Sun, 16 Aug 2020 01:36:36 +0300 Subject: [PATCH] Revert "yieldAsync() (#120)" This reverts commit 284d6778155fa956a773613b56b32b2522365849. There is no rough consensus for how this should be implemented and whether it's needed indeed - a use case that cannot be solved with a queue or a lock/event should be identified before pursuing this functionality. --- README.md | 47 ------------------------------------------- chronos/asyncloop.nim | 4 ---- tests/testfut.nim | 35 -------------------------------- 3 files changed, 86 deletions(-) diff --git a/README.md b/README.md index 10484bc..54a800f 100644 --- a/README.md +++ b/README.md @@ -242,53 +242,6 @@ other specific exception, in order to avoid catching by mistake `CancelledError` (object of `Exception`, used internally to propagate cancellation). -### Yielding - -If you need to give control back to the event loop, but there's no future you -need to `await` at that point, you can call `yieldAsync()` - a simple wrapper -around `sleepAsync()`: - -```nim -template yieldAsync*() = - await sleepAsync(0.seconds) -``` - -This comes in handy when splitting a blocking task into smaller parts: - -```nim -proc showMsg(after: int) {.async.} = - echo "begin showMsg()" - await sleepAsync(after.seconds) - echo "msg: after = ", after - -let maxDuration = 10 - -proc heavy() {.async.} = - echo "begin showMsg(", after, ")" - let start = Moment.now() - var s: uint64 - while true: - s += 1 - - # try commenting this out and see what happens - if s mod 1000000 == 0: - yieldAsync() - - if (Moment.now() - start).seconds >= (maxDuration + 1): - break - echo "sum: ", s - -proc p1() {.async.} = - var futures: seq[Future[void]] - - for after in 1..maxDuration: - futures.add(showMsg(after)) - futures.add(heavy()) - await allFutures(futures) - -waitFor p1() -``` - ## TODO * Pipe/Subprocess Transports. * Multithreading Stream/Datagram servers diff --git a/chronos/asyncloop.nim b/chronos/asyncloop.nim index b97a9d7..405c302 100644 --- a/chronos/asyncloop.nim +++ b/chronos/asyncloop.nim @@ -891,10 +891,6 @@ proc sleepAsync*(ms: int): Future[void] {. inline, deprecated: "Use sleepAsync(Duration)".} = result = sleepAsync(ms.milliseconds()) -# This can't be an async proc, because its `await` needs to modify the caller. -template yieldAsync*() = - await sleepAsync(0.seconds) - proc withTimeout*[T](fut: Future[T], timeout: Duration): Future[bool] = ## Returns a future which will complete once ``fut`` completes or after ## ``timeout`` milliseconds has elapsed. diff --git a/tests/testfut.nim b/tests/testfut.nim index 73a1cd5..d0e4bc6 100644 --- a/tests/testfut.nim +++ b/tests/testfut.nim @@ -969,38 +969,6 @@ suite "Future[T] behavior test suite": proc testCancellationRace(): bool = waitFor(testCancellationRaceAsync()) - proc testYieldAsync(): Future[bool] {.async.} = - proc showMsg(after: int, results: ptr seq[string]) {.async.} = - await sleepAsync(after.seconds) - results[].add("after " & $after) - - let maxDuration = 2 - - proc heavy(results: ptr seq[string]) {.async.} = - let start = Moment.now() - var s: uint64 - while true: - s += 1 - if s mod 1000000 == 0: - yieldAsync() - if (Moment.now() - start).seconds >= (maxDuration + 1): - break - results[].add("heavy") - - var - futures: seq[Future[void]] - results: seq[string] - - for after in 1..maxDuration: - futures.add(showMsg(after, results.addr)) - futures.add(heavy(results.addr)) - await allFutures(futures) - - return results == @["after 1", "after 2", "heavy"] - - proc testYield(): bool = - waitFor(testYieldAsync()) - test "Async undefined behavior (#7758) test": check test1() == true test "Immediately completed asynchronous procedure test": @@ -1054,6 +1022,3 @@ suite "Future[T] behavior test suite": check testWithTimeout() == true test "Cancellation race test": check testCancellationRace() == true - test "Yielding test": - check testYield() == true -