Non-blocking stuff
Hi All, I'm looking for some advice/pointers on having stuff non-block in my code. I have an application which runs on Dancer2 and is ~12,000 lines of code. It is actually (currently) four separate applications. It works really really well at the moment but for one thing, and that is that it continually blocks on pretty much everything. I've tried various solutions including starman which doesn't seem to run its allocated 10 workers for some reason. I've seen the model of nodejs and I'm looking for something similar to the call-back event non-blocking model of nodejs. I've tried using the apache road but that seems to recompile on every request, which adds ~ 1.5 seconds to each request, since that is the application startup time. Keen to see how other people solve this. Thanks, David
On Nov 16, 2015, at 5:05 PM, David H <untg99@gmail.com> wrote:
I'm looking for some advice/pointers on having stuff non-block in my code.
https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#Delayed...
Thanks, I tried that with this sample code. get '/TestingAssync' => sub { delayed { add_header 'X-Foo' => 'Bar'; flush; content 'Hello, world'; done; }; }; And I got this error: String found where operator expected near "add_header 'X-Foo'" (Do you need to predeclare add_header?) ? On Tue, Nov 17, 2015 at 10:53 AM, Warren Young <wyml@etr-usa.com> wrote:
On Nov 16, 2015, at 5:05 PM, David H <untg99@gmail.com> wrote:
I'm looking for some advice/pointers on having stuff non-block in my
code.
https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#Delayed... _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Also, I'm using version: Dancer2-0.163000 On Tue, Nov 17, 2015 at 11:39 AM, David H <untg99@gmail.com> wrote:
Thanks, I tried that with this sample code.
get '/TestingAssync' => sub { delayed { add_header 'X-Foo' => 'Bar'; flush; content 'Hello, world'; done; }; }; And I got this error:
String found where operator expected near "add_header 'X-Foo'" (Do you need to predeclare add_header?)
?
On Tue, Nov 17, 2015 at 10:53 AM, Warren Young <wyml@etr-usa.com> wrote:
On Nov 16, 2015, at 5:05 PM, David H <untg99@gmail.com> wrote:
I'm looking for some advice/pointers on having stuff non-block in my
code.
https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#Delayed... _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Nov 16, 2015, at 6:09 PM, David H <untg99@gmail.com> wrote:
add_header 'X-Foo' => 'Bar’;
That’s probably an old DSL keyword. It looks like it was replaced with ‘header’: https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#header Anyway, do you really need to set custom HTTP headers here? It’s just an example. If your app doesn’t need that, remove the line entirely.
Thanks, I've changed the custom headers. There is something which is causing a "400 Bad Response". I've narrowed it down to this simple use case: get '/Load/:ID' => sub { my $ID = params->{'ID'}; delayed { my $TextToWrite = ReturnText(); # Just returns "hi", works fine. my $Username = "Test"; session 'Username' => $Username; # Causes the whole thing to to return "400 Bad Response" and not work at all. header 'Content-Type' => "text/html; charset=UTF-8"; flush; content "$TextToWrite"; done; }; }; Notice the "session 'Username' => $Username." That causes a "400 Bad Response" error. I'm guessing something to do with the session being set and returning before other stuff has happened?? Thanks, David On Tue, Nov 17, 2015 at 11:43 AM, Warren Young <wyml@etr-usa.com> wrote:
On Nov 16, 2015, at 6:09 PM, David H <untg99@gmail.com> wrote:
add_header 'X-Foo' => 'Bar’;
That’s probably an old DSL keyword. It looks like it was replaced with ‘header’:
https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#header
Anyway, do you really need to set custom HTTP headers here? It’s just an example. If your app doesn’t need that, remove the line entirely. _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
HI All, So I've solved the issue with sessions and it appears as though it is mucking up the delayed subroutine and I've written a test case based on the example in the docs and I'm surely missing something as it doesn't work properly, since it blocks. Here is a simple case of delayed blocking, I've moved the session code outside the subroutine which now does return. get '/Load/:ID' => sub { my $ID = params->{'ID'}; my $Username = "Test"; session 'Username' => $Username; delayed { my $TextToWrite = ReturnText(); sleep(10); # Do something that takes a few seconds content "$TextToWrite"; done; }; }; at the "sleep" function, the application basically stops, waiting for sleep to complete. The docs confused me as well, it says: content 'Hello, again!'; # when done, close the connection done; # do whatever you want else, asynchronously # the user socket closed by now ... }; }; At the end it talks about doing stuff asynchronously, implying that whatever was done previously was not asynchronous? So yeah, some clarification would be awesome, or just someone with an example which works. David On Tue, Nov 17, 2015 at 1:56 PM, David H <untg99@gmail.com> wrote:
Thanks, I've changed the custom headers. There is something which is causing a "400 Bad Response".
I've narrowed it down to this simple use case: get '/Load/:ID' => sub { my $ID = params->{'ID'}; delayed { my $TextToWrite = ReturnText(); # Just returns "hi", works fine. my $Username = "Test"; session 'Username' => $Username; # Causes the whole thing to to return "400 Bad Response" and not work at all. header 'Content-Type' => "text/html; charset=UTF-8"; flush; content "$TextToWrite"; done;
}; };
Notice the "session 'Username' => $Username."
That causes a "400 Bad Response" error. I'm guessing something to do with the session being set and returning before other stuff has happened??
Thanks,
David
On Tue, Nov 17, 2015 at 11:43 AM, Warren Young <wyml@etr-usa.com> wrote:
On Nov 16, 2015, at 6:09 PM, David H <untg99@gmail.com> wrote:
add_header 'X-Foo' => 'Bar’;
That’s probably an old DSL keyword. It looks like it was replaced with ‘header’:
https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#header
Anyway, do you really need to set custom HTTP headers here? It’s just an example. If your app doesn’t need that, remove the line entirely. _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Nov 16, 2015, at 9:09 PM, David H <untg99@gmail.com> wrote:
So yeah, some clarification would be awesome, or just someone with an example which works.
One of the items on the list of ideas for Advent Calendar 2015 articles is exactly that: https://github.com/PerlDancer/advent-calendar Since no one has signed up to write that article, maybe you should. I know you don’t know how to make it work right now, but the code is open, so you could in principle figure out how to use it. An article written from the perspective of a newbie is often better in the end than one written by someone who knows how the feature is *supposed* to work, because you’re more likely to remember where all the traps are. I might write it myself, because I, too, have not yet used this feature of Dancer 2, but it was already on my to-do list. (I want to use the feature to throttle repeated login attempts without slowing down one-off logins.) That said, I’ve already written 6 articles for the calendar, and have received hardly any good feedback, so maybe I should leave the remaining 3/4 of the available spots to others.
Thanks Warren, I could fiddle with it, I've spent a good 30 mins to 1 hour trying to get various versions of the code working without success. I can attest the the 'streaming' part works fine and the code will stream properly (I even cracked out Wireshark to test), however it still blocks. In terms of the non-blocking part, I'm sure there is something fundamental I'm doing wrong, just not sure what. David On Tue, Nov 17, 2015 at 2:50 PM, Warren Young <wyml@etr-usa.com> wrote:
On Nov 16, 2015, at 9:09 PM, David H <untg99@gmail.com> wrote:
So yeah, some clarification would be awesome, or just someone with an
example which works.
One of the items on the list of ideas for Advent Calendar 2015 articles is exactly that:
https://github.com/PerlDancer/advent-calendar
Since no one has signed up to write that article, maybe you should. I know you don’t know how to make it work right now, but the code is open, so you could in principle figure out how to use it.
An article written from the perspective of a newbie is often better in the end than one written by someone who knows how the feature is *supposed* to work, because you’re more likely to remember where all the traps are.
I might write it myself, because I, too, have not yet used this feature of Dancer 2, but it was already on my to-do list. (I want to use the feature to throttle repeated login attempts without slowing down one-off logins.)
That said, I’ve already written 6 articles for the calendar, and have received hardly any good feedback, so maybe I should leave the remaining 3/4 of the available spots to others. _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
I've been pretty busy of late so had no time to keep track of the list. My apologies. Still, I would be happy to try and resolve this. Since this was last month, I'm assuming there might have been changes. Can you tell me what the last current state (plus some code, please) of this is? We'll pick it up from the current state and try and resolve any issues. On Tue, Nov 17, 2015 at 8:55 AM, David H <untg99@gmail.com> wrote:
Thanks Warren,
I could fiddle with it, I've spent a good 30 mins to 1 hour trying to get various versions of the code working without success. I can attest the the 'streaming' part works fine and the code will stream properly (I even cracked out Wireshark to test), however it still blocks. In terms of the non-blocking part, I'm sure there is something fundamental I'm doing wrong, just not sure what.
David
On Tue, Nov 17, 2015 at 2:50 PM, Warren Young <wyml@etr-usa.com> wrote:
On Nov 16, 2015, at 9:09 PM, David H <untg99@gmail.com> wrote:
So yeah, some clarification would be awesome, or just someone with an example which works.
One of the items on the list of ideas for Advent Calendar 2015 articles is exactly that:
https://github.com/PerlDancer/advent-calendar
Since no one has signed up to write that article, maybe you should. I know you don’t know how to make it work right now, but the code is open, so you could in principle figure out how to use it.
An article written from the perspective of a newbie is often better in the end than one written by someone who knows how the feature is *supposed* to work, because you’re more likely to remember where all the traps are.
I might write it myself, because I, too, have not yet used this feature of Dancer 2, but it was already on my to-do list. (I want to use the feature to throttle repeated login attempts without slowing down one-off logins.)
That said, I’ve already written 6 articles for the calendar, and have received hardly any good feedback, so maybe I should leave the remaining 3/4 of the available spots to others. _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Sat, Dec 26, 2015 at 8:27 PM, Sawyer X <xsawyerx@gmail.com> wrote:
I've been pretty busy of late so had no time to keep track of the list. My apologies.
Still, I would be happy to try and resolve this.
Since this was last month, I'm assuming there might have been changes. Can you tell me what the last current state (plus some code, please) of this is? We'll pick it up from the current state and try and resolve any issues.
I guess the basic issue that happens is what is described above and for some reason for me, Starman doesn't seem to solve it. A basic example I will give is this, and keep in mind, this is a single user, not multiple at this stage. - Load up the home page of the website, there are processes that run that might take 4-5 seconds to complete in the background (using jquery) (let's call it /LoadPage). - The I click on a page to generate a report (say /LoadReport), however, the report waits for /LoadPage to fully complete before starting (on the server side, I can see the client sending through the request immediately), thus it is completely blocking. It seems to do this even when running through Starman. I'm not sure if there is a way (or if this even makes sense) to fork out each request so the dancer site isn't just sitting there waiting for the last thing to complete. I tried using the Delayed responses (Async/Streaming) listed here: https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#Delayed... But for some reason, maybe because it is solving a problem that I don't specifically have, it didn't seem to help. Also, I pretty much all the time run with -R (or the equivalent restart switch for plackup/starman) so the site restarts without me having to intervene if any of the files get modified, I'm not sure if that factors into the forking issue I have. Thanks, David
On Tue, Nov 17, 2015 at 8:55 AM, David H <untg99@gmail.com> wrote:
Thanks Warren,
I could fiddle with it, I've spent a good 30 mins to 1 hour trying to get various versions of the code working without success. I can attest the the 'streaming' part works fine and the code will stream properly (I even cracked out Wireshark to test), however it still blocks. In terms of the non-blocking part, I'm sure there is something fundamental I'm doing wrong, just not sure what.
David
On Tue, Nov 17, 2015 at 2:50 PM, Warren Young <wyml@etr-usa.com> wrote:
On Nov 16, 2015, at 9:09 PM, David H <untg99@gmail.com> wrote:
So yeah, some clarification would be awesome, or just someone with an example which works.
One of the items on the list of ideas for Advent Calendar 2015 articles
is
exactly that:
https://github.com/PerlDancer/advent-calendar
Since no one has signed up to write that article, maybe you should. I know you don’t know how to make it work right now, but the code is open, so you could in principle figure out how to use it.
An article written from the perspective of a newbie is often better in the end than one written by someone who knows how the feature is *supposed* to work, because you’re more likely to remember where all the traps are.
I might write it myself, because I, too, have not yet used this feature of Dancer 2, but it was already on my to-do list. (I want to use the feature to throttle repeated login attempts without slowing down one-off logins.)
That said, I’ve already written 6 articles for the calendar, and have received hardly any good feedback, so maybe I should leave the remaining 3/4 of the available spots to others. _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Sat, 2016-01-09 at 00:09 +1030, David H wrote:
I'm not sure if there is a way (or if this even makes sense) to fork out each request so the dancer site isn't just sitting there waiting for the last thing to complete.
Really stupid question, but I assume you do have Starman configured for multiple processes accepting requests on the server-side? Otherwise you would of course see the behaviour you describe.
I tried using the Delayed responses (Async/Streaming) listed here:
I would expect that. Delayed is still single-threaded (?), it just allows responses to be sent part way through processing request (I think, I've not looked at it in detail). I certainly would have thought that Starman or other multi-threaded server is the way to go. I do have some resource-intensive processes in one of my apps (that produce data for the same webpage via a separate request), and I've never noticed a problem. I've never looked at it in detail though. I'm using fast-cgi in Apache. Andy
Could you provide some sample code so I could play with it? Also, how are you implementing the delayed server? What event loop are you using? Are you trying to do fully non-blocking asynchronous or are you just having a delayed response? There is a big difference. The problem with delayed responses is that there are several options and in between them you need to understand how they work, what they do, event loop, the effect *on* your code, the effect *of* your code etc. That's why it's a pretty advanced feature that is usually unnecessary and the usage of it should probably be very well contained. On Fri, Jan 8, 2016 at 5:55 PM, Andrew Beverley <andy@andybev.com> wrote:
On Sat, 2016-01-09 at 00:09 +1030, David H wrote:
I'm not sure if there is a way (or if this even makes sense) to fork out each request so the dancer site isn't just sitting there waiting for the last thing to complete.
Really stupid question, but I assume you do have Starman configured for multiple processes accepting requests on the server-side? Otherwise you would of course see the behaviour you describe.
I tried using the Delayed responses (Async/Streaming) listed here:
I would expect that. Delayed is still single-threaded (?), it just allows responses to be sent part way through processing request (I think, I've not looked at it in detail).
I certainly would have thought that Starman or other multi-threaded server is the way to go.
I do have some resource-intensive processes in one of my apps (that produce data for the same webpage via a separate request), and I've never noticed a problem. I've never looked at it in detail though. I'm using fast-cgi in Apache.
Andy
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Wed, Jan 13, 2016 at 4:49 AM, Sawyer X <xsawyerx@gmail.com> wrote:
Could you provide some sample code so I could play with it?
Also, how are you implementing the delayed server? What event loop are you using? Are you trying to do fully non-blocking asynchronous or are you just having a delayed response? There is a big difference.
The problem with delayed responses is that there are several options and in between them you need to understand how they work, what they do, event loop, the effect *on* your code, the effect *of* your code etc. That's why it's a pretty advanced feature that is usually unnecessary and the usage of it should probably be very well contained.
So, after writing basic test app, then retesting my own code, it appears the issue I was having is resolved. For the record, my test app is below, which I've tested with --workers 1 and --workers 3 and --workers 1 will block, whereas --workers 3 works with 3 children. The method I use is to go to http://servername:5001/ Then in another tab, go to http://servername:5001/PlainRoute while / is running. Now, for some reason, the whole thing works and it will just server up /PlainRoute even if / is still going on doing its stuff. It may be that in some of my routes, there is some code that blocks starman, I don't know, so I will keep an eye on this and if I find a situation I willgo from there. This is my test app and startup command: plackup -s Starman -p 5001 ./bin/app.psgi This is the basic code, connecting to MongoDB and doing some stuff: package TestApp; use Dancer2; use MongoDB; our $VERSION = '0.1'; sub connect_MongoDB_referral_db { my $database; my $client = MongoDB::MongoClient->new( host => 'mongodb://user:pass@localhost/ICSIntakeReferralsDev', auto_connect => 1, query_timeout => -1, ); $database = $client->get_database('ICSIntakeReferralsDev'); } return $database; } get '/PlainRoute' => sub { template 'index'; }; get '/' => sub { my %Data; for (1..100) { my $MongoDBConnection = connect_MongoDB_referral_db(); my $MongoReturnData = $MongoDBConnection->get_collection('Patients')->find; while ( my $MongoReturnObject = $MongoReturnData->next ) { my $IDObj = $MongoReturnObject->{'_id'}; my $IDString = $IDObj->to_string; print "ID string is $IDString\n"; $Data{$IDString}++; } } template 'index'; }; true; This is the pretty picture version: [image: Inline image 1] So, as mentioned, it appears that it is all working now but I'll keep monitoring closely. Thanks for all your help everyone. David On Fri, Jan 8, 2016 at 5:55 PM, Andrew Beverley <andy@andybev.com> wrote:
On Sat, 2016-01-09 at 00:09 +1030, David H wrote:
I'm not sure if there is a way (or if this even makes sense) to fork out each request so the dancer site isn't just sitting there waiting for the last thing to complete.
Really stupid question, but I assume you do have Starman configured for multiple processes accepting requests on the server-side? Otherwise you would of course see the behaviour you describe.
I tried using the Delayed responses (Async/Streaming) listed here:
I would expect that. Delayed is still single-threaded (?), it just allows responses to be sent part way through processing request (I think, I've not looked at it in detail).
I certainly would have thought that Starman or other multi-threaded server is the way to go.
I do have some resource-intensive processes in one of my apps (that produce data for the same webpage via a separate request), and I've never noticed a problem. I've never looked at it in detail though. I'm using fast-cgi in Apache.
Andy
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
participants (4)
-
Andrew Beverley -
David H -
Sawyer X -
Warren Young