Mojo::Server::Threaded - Multithreaded non-blocking I/O HTTP and WebSocket server
use Mojo::Server::Threaded; my $threaded = Mojo::Server::Threaded->new(listen => ['http://*:8080']); $threaded->unsubscribe('request')->on(request => sub { my ($threaded, $tx) = @_; # Request my $method = $tx->req->method; my $path = $tx->req->url->path; # Response $tx->res->code(200); $tx->res->headers->content_type('text/plain'); $tx->res->body("$method request for $path!"); # Resume transaction $tx->resume; }); $threaded->run;
Mojo::Server::Threaded is a multithreaded alternative for Mojo::Server::Prefork which is not available under Win32. It takes the same parameters and works analoguous by just using threads instead of forked processes.
The main difference besides using threads is that signals are only used for the termination of the server. Starting, stopping and de- or increasing the amount of workers is done via "send_command".
Signals under Windows are not really useful for communicating with running background processes. Mojo::Server::Threaded can be controlled using a TCP management socket. See "MANAGER COMMANDS".
The Mojo::Server::Threaded manager process can be controlled at runtime with the following signals.
Shut down server immediately.
Shut down server gracefully.
Mojo::Server::Threaded emits the same events as Mojo::Server::Prefork and implements the following new ones.
$threaded->on(command => sub { my($threaded, $command, @params) = @_; say "got command $command on management port"; ... });
Emitted when a command is received on the manager listening port.
Mojo::Server::Threaded recognizes the same attributes as Mojo::Server::Prefork and implements the following new ones.
my $interval = $threaded->manage_interval; $threaded = $threaded->manage_interval(0.5);
Check interval for the management port in seconds, defaults to 0.1.
0.1
Mojo::Server::Threaded inherits all methods from Mojo::Server::Daemon, implements the same ones as Mojo::Server::Prefork and the following new ones.
my $port = $threaded->check_mport;
Get the TCP port for the management socket from "pid_file" in Mojo::Server::Prefork.
$threaded->register_command( HELLO => sub { my($self, $cmd, @params) = @_; warn "handling $cmd on management port\n"; });
Register a management command and handler subroutine. The command will be upcased.
$threaded->send_command("hello DO SOMETHING");
Send a command to the management socket. The string will be split on whitespace and the first element will be upcased on the receiver's side.
Mojo::Server::Threaded can be controlled by the following commands:
Send QUIT or KILL to an existing worker thread
Increase or decrease (negative values) the number of workers by NUMBER.
Since Perl ithreads are relatively heavy in comparison the spawning of threads is much slower than forking on UNIX. The hot deploy also will be much slower, as a new interpreter is started.
Once running this should not affect performance too much.
Mojolicious, Mojolicious::Guides, https://mojolicious.org.
To install Mojo::Server::Threaded, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::Server::Threaded
CPAN shell
perl -MCPAN -e shell install Mojo::Server::Threaded
For more information on module installation, please visit the detailed CPAN module installation guide.