use threads; use Thread::Queue; my $command_queue=Thread::Queue->new(); sub handle_request { my ($self, $cgi) = @_; my $cmd_out; ........ $command_queue->enqueue($cmd_out); my $commands_left=$command_queue->pending(); myDebug("Threads - there are $commands_left items in the queue"); } my $server_thread=threads->create( sub { # Run server but do not background it. myDebug("Starting web server\n"); my $pid = MyServer->new($PORT)->run(); } } while($quit==0) { my $commands_left=$command_queue->pending(); myDebug("Queue runner - There are $commands_left entries in the queue"); if($commands_left>0) { print "We have commands to process\n"; $send_command=$command_queue->dequeue(); print "Sending: $send_command\n"; } # Sleep so we do not hog the processor sleep(1); }