Non-blocking IO understands which part is non-blocking

The web server that Mojolicious has supports non-blocking IO. However, when it comes to non-blocking, I think that each person has various images, and I feel that there are many misunderstandings, so I will explain briefly.

The non-blocking part is the part that reads and writes the HTTP request

What makes Mojolicious non-blocking is the part that reads and writes HTTP requests. So, for example, if you want to return multiple 10MB files as a response, you can process them in parallel. Also, when uploading multiple 10MB files as a request, they can be processed in parallel.

Database access is not non-blocking

Database access is generally not non-blocking. Accessing MySQL and SQLite is a blocking process.

So, just because you're using Mojolicious doesn't mean that everything is non-blocking, it blocks the part that accesses the database.

For example, if there is a process that takes 30 seconds to process, it will block for 30 seconds at that part. Subsequent processing cannot be performed while waiting for this.

If so, it's probably better to run it as a pure prefolk server rather than as a non-blocking IO web server (I haven't tried it yet). Decrease the number of clients in parallel processing and increase the number of workers.

hypnotoad => {
  clients => 1,
  workers => 10
}

When using Mojolicious, keep in mind that if there is a long blocking process in the process, the default settings will cause subsequent processing to stop.

How to make database access non-blocking

This is a difficult problem and cannot be easily solved by common methods. In terms of architecture, there is no choice but to incorporate the processing in one IO loop used internally by the framework, but this is very difficult and each database module must support it. I have.

The easiest way is to use the module provided by the author of Mojolicious to access MongoDB called Mango (still experimental). With the combination of Mojolicious + MongoDB, it is relatively easy to do all the processing non-blocking.

The process of reading a file is not non-blocking

Normally, when writing a process to read a file in Perl, it is not a non-blocking process but a blocking process. Therefore, if you want to write a content distribution server that requires a large amount of processing, you are not writing normal file input / output processing.

You need to install AE and use AnyEvent to write the part that reads the file non-blocking. If you don't do this, even if you can parallelize the HTTP request and response processing, it will block internally.

Associated Information