Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not calling setGlobalDispatcher newDispatcher() costs 500ms #6846

Closed
RedBeard0531 opened this issue Nov 30, 2017 · 3 comments
Closed

Not calling setGlobalDispatcher newDispatcher() costs 500ms #6846

RedBeard0531 opened this issue Nov 30, 2017 · 3 comments
Labels
Async Everything related to Nim's async

Comments

@RedBeard0531
Copy link
Contributor

import asyncdispatch
import asyncfile

var asyncStdout = 1.AsyncFD.newAsyncFile()
proc doStuff: Future[void] {.async.} =
  await asyncStdout.write "hello world\n"

#setGlobalDispatcher newDispatcher() # magical Go Fast button!
waitFor doStuff()

time: 502ms
time with dispatcher: 2ms

@andreaferretti andreaferretti added the Async Everything related to Nim's async label Nov 30, 2017
@kvinwang
Copy link
Contributor

kvinwang commented Dec 1, 2017

The iterator generated by async is:

  iterator doStuffIter245003(): FutureBase {.closure.} =
    var future245004 = asyncStdout.write "hello world\x0A"
    yield future245004
    future245004.read
    complete(retFuture245002)

But the future245004 completed immediately after asyncStdout.write.
Then the next yield will cause an extra polling, which will get nothing but 500ms timeout.
I guess the right code should looks like this:

  iterator doStuffIter245003(): FutureBase {.closure.} =
    var future245004 = asyncStdout.write "hello world\x0A"
    if not future245004.finished:
      yield future245004
    future245004.read
    complete(retFuture245002)

@yglukhov
Copy link
Member

yglukhov commented Dec 1, 2017

Will be fixed by #6614

@dom96
Copy link
Contributor

dom96 commented Aug 22, 2018

This appears to be fixed now.

Reproduces in 0.17.2 but not 0.18.0 nor devel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Async Everything related to Nim's async
Projects
None yet
Development

No branches or pull requests

5 participants