On Wed, Jan 15, 2014 at 4:48 PM, Ben van Staveren
<[email protected]>wrote:
> Okay, so... blocking/non-blocking 101 (sortakinda):
>
Thanks so much, Ben, for this 101 primer! It has really helped a lot!
Seems to me it would be worth while to take as is and put on Mojo's wiki.
At least there'd be a start to some non-blocking related information.
> When an operation is "blocking" what it literally means is that the
> operation blocks further execution of the program. For example opening a
> socket with Socket is a blocking operation, since the actual connection
> attempt blocks until a result is available. This also applies to files and
> reading data from them.
>
Does this mean that the actual connection attempt which blocks is
equivalent to waiting on a user to type some data at an <STDIN> prompt?
Socket is to <STDIN> as Mojo::IOLoop::Client is to Term::ReadKey. Is that
right?
And Socket blocks quite simply because the Socket author didn't write it to
take advantage of an event loop? Darn it -- why not?? :D Basically, this
is out of the scope of the purpose of Socket and someone else is invited to
come along and add the non-blocking functionality. Is that what
AnyEvent::Socket is? Or is AnyEvent::Socket a brand new implementation of
socket connecting but it just chooses to include event loop support in the
scope of its purpose?
An event loop basically boils down to:
>
> while(1) {
> do_some_timer_stuff;
> check_any_fds_for_readability;
> handle_readable_fds;
> check_any_fds_for_writabiliy;
> handle_writable_fds;
> }
>
This is really great to see -- how an event loop basically works. I had no
idea! Which of these pseudo-functions in the loop would be the most likely
place for the blocking to occur? For example, when Nacho tries to connect
to a Socket or when I am waiting for a response from a Database?
> So, you do still need to actually inform the event loop that it needs to
> check for something. Now Mojo::IOLoop does let you do this on plain
> "Socket" connections, but you'd have to get the file descriptor, then use
> Mojo::IOLoop's functionality to get it to be taken up into the checks.
>
> That's why Mojo::IOLoop->client works out of the box, because it does that
> part already, for you.
>
If I'm understanding correctly, Nacho's problem could be resolved by using
Mojo::IOLoop->client instead of Socket?
> But... you can in fact block inside an event loop.
>
Are there any tools to help pinpoint where blocking is occurring? For
example, when I wrote my first app connecting to a database, I had no
reason to suspect that the connection to the database is what was causing
the event loop to back up. Especially when I run a query on the mysql>
prompt and it tells me the query takes 0.00 seconds or 0.02 seconds at
most. The problem of course was when dozens of users were connecting to
the app and therefore the database concurrently and I had only a few
preforked processes at most.
> The above pattern is what you'd use to talk to a backend API over HTTP.
> You can also do this for MongoDB as long as you use the Mango driver
> (because sri made it and it's also designed to support non-blocking out of
> the box); for other things such as ZMQ or RabbitMQ you will have to find a
> module that implements the appropriate protocol *and* does things in a
> non-blocking fashion; for example AnyEvent::RabbitMQ can do it for RabbitMQ.
>
So if I really wanted to connect to a MySQL database non-blockingly -- much
like Mango is doing for MongoDB -- I could use Mojo::IOLoop->client to
connect to :3306 and get to work implementing the entire MySQL protocol!
Is that right? Likewise with connecting to an NTP server or anything else.
> Anyway, writing out stuff like this at 5am in the morning isn't going to
> make it super clear but it's the best I can do :P
Again, Ben, thanks so much for this response and especially for taking the
time to do so so early in the morning. I would request putting it on a
Mojo wiki in an effort to reduce the number of non-blocking related
messages and to give all of us poor folk who desire to write good apps but
just haven't got the background to get off to a good start.
--
*----------------------------------------------------------------*
*Keystone IT made the following notation*
*----------------------------------------------------------------*
*Email Confidentiality Notice: The information contained in this
transmission is confidential, proprietary or privileged and may be subject
to protection under the law, including the Health Insurance Portability and
Accountability Act (HIPAA). *
*This message is for the sole use of the intended individual or entity to
whom it is addressed. If you are not the intended recipient, you are
notified that any use, distribution or copying of the message is strictly
prohibited and may subject you to criminal or civil penalties. If you
received this transmission in error, please contact the sender immediately
at (314) 621-9500 and delete the material from all computers.*
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.