You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
+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?
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...
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.
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:
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
.
Reacted by Joseph Lennox, Aleksandar Dragojević, Stephen Cleary, Oliver Weichhold and Masters
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.
@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...
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:
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!
@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!
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.
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!
@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.
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).
Activity
ThomasGe commentedon Dec 11, 2013
This is a really great feature!
toddlucas commentedon Dec 13, 2013
+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?
roji commentedon Dec 13, 2013
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.
roji commentedon Jan 25, 2014
Good article on column async: http://blogs.msdn.com/b/adonet/archive/2012/04/20/using-sqldatareader-s-new-async-methods-in-net-4-5-beta.aspx
Would probably entail some considerable refactoring...
cthames commentedon Apr 12, 2014
+1
annymsMthd commentedon May 23, 2014
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?
franciscojunior commentedon May 25, 2014
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.
glenebob commentedon May 25, 2014
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:
annymsMthd commentedon May 25, 2014
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.
roji commentedon Jul 5, 2014
@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...
glenebob commentedon Jul 5, 2014
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:
franciscojunior commentedon Jul 5, 2014
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:
94 remaining items
roji commentedon Apr 4, 2015
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!
bullgoose311 commentedon Apr 8, 2015
@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!
roji commentedon Apr 12, 2015
Mini task list:
FransBouma commentedon Apr 12, 2015
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!
roji commentedon Apr 13, 2015
@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.
roji commentedon May 14, 2015
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).
dcolakchile commentedon Jun 16, 2019
Hey guys, did we or did we not get the ASYNC version of the library?
YohDeadfall commentedon Jun 17, 2019
There is no async version because ASO.NET has asynchronous API which we supports since version 3.0. All of these methods have
Asyncsuffix.