Skip to content

Implement async #121

Closed
Closed

Description

@roji
No description provided.

Activity

  1. ThomasGe commented on Dec 11, 2013

    @ThomasGe

    This is a really great feature!

  2. toddlucas commented on Dec 13, 2013

    @toddlucas

    +1 This will greatly improve throughput for web apps, since the database is where threads often block for the majority of the time during a request. Any chance it will be considered for 2.1?

  3. roji commented on Dec 13, 2013

    @roji
    MemberAuthor

    2.1 is probably going to go into beta sometime next week, so async is definitely out of scope for that... But hopefully 2.2 won't take an enormous amount of time...

    Thanks for commenting.

  4. modified the milestones: , on Apr 4, 2014
  5. cthames commented on Apr 12, 2014

    @cthames

    +1

  6. annymsMthd commented on May 23, 2014

    @annymsMthd

    What do you think the level of effort is for this feature? If I was interested in looking into it where would be a good place to start?

  7. franciscojunior commented on May 25, 2014

    @franciscojunior
    Member

    A good place to start would be NpgsqlState.cs file in the ProcessBackendResponses method: https://github.com/npgsql/Npgsql/blob/894f3fc3085e6992febef74e24e530344d911454/Npgsql/Npgsql/NpgsqlState.BackendResponse.cs#L108

    The idea would be to break the NpgsqlCommand.ExecuteReader() in two parts: The BeginExecuteReader and EndExecuteReader.

    Each part would be handled by a different thread. One would take care of sending the query to the server and would return to the client (this would be the client thread, which calls the BeginExecuteReader)

    Then, there would be the "NpgsqlQueryProcessing" thread which keep waiting for data to be received and would prepare the data to be consumed by the client thread when it calls the EndExecuteReader().

    This way I think we could implement the async calls.

    I think we have almost everything in place. We just need to add some thread synchronization in place because today all the server handling is done in the client thread. With async in place, we would need to work in the communication between the client thread and the "NpgsqlQueryProcessing" thread.

  8. glenebob commented on May 25, 2014

    @glenebob
    Contributor

    The very simplest way to implement async would be to create a function like
    so:

    public Task ExecuteReaderAsync()
    {
    return Task.Run(() => ExecuteReader());
    }

    Then, call it like so:

    Task queryTask = command.ExecuteReaderAsync();

    queryTask.Wait();

    using (queryTask.Result)
    {
    while (queryTask.Result.Read())
    {
    }
    }

    You could also wrap DataReader.Read() in a similar method to make that part
    async if needed.

    IMO, this is a bit too simplistic for an Npgsql implementation, but in the
    absence of an Npgsql implementation, it's very very easy for user code to
    handle it the way I demonstrated.

    In Npgsql, an async implementation should also solve, properly and
    permenantly, the problem with async notifications, and what I layed out
    above does nothing to solve that.

    The benefit to doing what I demonstrated is that nothing happens on the
    caller's thread. Processing the query string and params, sending the query
    to the backend, and waiting for the response are all done in a background
    thread, so the application could before extremely well. And it's easy :)

    On Sun, May 25, 2014 at 9:18 AM, Francisco Figueiredo Jr. <
    [email protected]> wrote:

    A good place to start would be NpgsqlState.cs file in the
    ProcessBackendResponses method:
    https://github.com/npgsql/Npgsql/blob/894f3fc3085e6992febef74e24e530344d911454/Npgsql/Npgsql/NpgsqlState.BackendResponse.cs#L108

    The idea would be to break the NpgsqlCommand.ExecuteReader() in two parts:
    The BeginExecuteReader and EndExecuteReader.

    Each part would be handled by a different thread. One would take care of
    sending the query to the server and would return to the client (this would
    be the client thread, which calls the BeginExecuteReader)

    Then, there would be the "NpgsqlQueryProcessing" thread which keep waiting
    for data to be received and would prepare the data to be consumed by the
    client thread when it calls the EndExecuteReader().

    This way I think we could implement the async calls.

    I think we have almost everything in place. We just need to add some
    thread synchronization in place because today all the server handling is
    done in the client thread. With async in place, we would need to work in
    the communication between the client thread and the "NpgsqlQueryProcessing"
    thread.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/121#issuecomment-44138687
    .

  9. annymsMthd commented on May 25, 2014

    @annymsMthd

    The big problem with return Task.Run(() => ExecuteReader()); is that you are still using a thread to poll the underlying socket. To implement a true async solution you would need to have async all the way through the stack. Instead of polling the socket we would need to use the socket's async features to allow us to use an io completion port and save the cpu from polling the connection.

    I've looked through some of the code and I'm starting to get a grasp on what's going on. The biggest cpu user I'm currently seeing is the Socket.Poll in NpgsqlState.CheckForContextSocketAvailability. I'm wondering if this is a good place to start. Also overriding ExecuteDbDataReaderAsync in NpgsqlCommand.

  10. roji commented on Jul 5, 2014

    @roji
    MemberAuthor

    @glenebob, @franciscojunior, @annymsMthd, is anyone actively working or planning to work on async? If not I think I might want to take a stab at it. Let me know...

  11. glenebob commented on Jul 5, 2014

    @glenebob
    Contributor

    Hey Shay,

    There's no way I can dive into this right now. Have at it, I say!

    -Glen

    On Sat, Jul 5, 2014 at 3:15 AM, Shay Rojansky [email protected]
    wrote:

    @glenebob https://github.com/glenebob, @franciscojunior
    https://github.com/franciscojunior, @annymsMthd
    https://github.com/annymsMthd, is anyone actively working or planning
    to work on async? If not I think I might want to take a stab at it. Let me
    know...

    —
    Reply to this email directly or view it on GitHub
    #121 (comment).

  12. franciscojunior commented on Jul 5, 2014

    @franciscojunior
    Member

    Hi, Shay!

    There is no one working on this yet.

    I think this will be an excellent feature!

    Please, let me know if you need any help.

    I'm working in the process of 2.2 release and as soon as I finish it, I'd
    be glad to help you with that.
    Em 05/07/2014 17:24, "Glen Parker" [email protected] escreveu:

    Hey Shay,

    There's no way I can dive into this right now. Have at it, I say!

    -Glen

    On Sat, Jul 5, 2014 at 3:15 AM, Shay Rojansky [email protected]
    wrote:

    @glenebob https://github.com/glenebob, @franciscojunior
    https://github.com/franciscojunior, @annymsMthd
    https://github.com/annymsMthd, is anyone actively working or planning
    to work on async? If not I think I might want to take a stab at it. Let
    me
    know...

    —
    Reply to this email directly or view it on GitHub
    #121 (comment).

    —
    Reply to this email directly or view it on GitHub
    #121 (comment).

  13. 94 remaining items

  14. added this to the 3.0-beta1 milestone on Apr 4, 2015
  15. reopened this on Apr 4, 2015
  16. roji commented on Apr 4, 2015

    @roji
    MemberAuthor

    OK, we ended up going for a much more serious rewrite of Npgsql for v3. One major part of this rewrite is a new type handling system specifically designed around performance, and a general separation of I/O logic from non-I/O logic, to help with async. We're starting to near the point where a beta of v3 can be released.

    Because of all this work another final chunk of work on async has to take place: go over all I/O flows and make sure the async flow is fully async, separate out the AsyncGenerator into another project and integrate it in Npgsql via a standard nuget, update to the latest version of Roslyn in the AsyncGenerator.

    Since we had so much discussion on async implementation, I wanted to sound out everyone before jumping in and finishing the job. @FransBouma specifically, you really helped out with valuable advice on this before. Although I/O and non-I/O logic has been separated to a large extent, there still exists a lot of I/O logic that needs to be duplicated, once for sync and once for async; a good example might be NpgsqlDataReader.NextResultInternal(). I can't see a way to refactor this for sharing between the sync and async code path (not in a meaningful way, in any case), and so the AsyncGenerator (generate an async version of the method based on its sync source) still seems like the best approach. Any ideas on doing this better?

    @FransBouma, if you have any spare time it would be great to get your feedback on the new Npgsql!

  17. bullgoose311 commented on Apr 8, 2015

    @bullgoose311

    @roji and everyone else on this thread, just wanted to say I created a github account simply so I could post how awesome this discussion was. Came here with a fair understanding of async in .NET and questions regarding Npgsql's support for it. 2 hours later I've come away with a much stronger grasp on async in .NET and an energized passion for open source. Super excited for Npgsql v3, great work!

  18. roji commented on Apr 12, 2015

    @roji
    MemberAuthor

    Mini task list:

    • Separate out AsyncGenerator to external project
    • Update to latest version of Roslyn
    • Go over all flows and make sure there's no sync left in async
    • Frontend (socket) timeouts and async. Find out what is required here, should we be adding an async timeout? Also make sure our async notification mechanism (which uses async) works well.
  19. FransBouma commented on Apr 12, 2015

    @FransBouma
    Contributor

    Hey @roji :) I'm afraid I have no time to spent on this in the coming weeks :(. I'm sorry to let you down, after the work you've done on this. I've asked on twitter whether there's a person out there who can help out. I'll see if I can find an hour or so this week to look at some code at least.

    Hats off to you on the massive amount of work done!

  20. roji commented on Apr 13, 2015

    @roji
    MemberAuthor

    @FransBouma, it's nice to hear from you again, and thanks for tweeting about us!

    Don't worry about being busy. If you do have a spare hour or two, I'm especially interested in your opinion on the validity (maybe even the necessity) of the AsyncGenerator code generation approach to async. I'll go ahead and keep working with that approach for now, but it would be good to get your opinion.

  21. roji commented on May 14, 2015

    @roji
    MemberAuthor

    Opened separate issue for async frontend timeouts: #607. AFAIK async support should now be complete (apart from async connection support which will happen for 3.1).

  22. modified the milestones: 3.0-beta1, on Aug 7, 2015
  23. dcolakchile commented on Jun 16, 2019

    @dcolakchile

    Hey guys, did we or did we not get the ASYNC version of the library?

  24. YohDeadfall commented on Jun 17, 2019

    @YohDeadfall
    Contributor

    There is no async version because ASO.NET has asynchronous API which we supports since version 3.0. All of these methods have Async suffix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions