in reply to Re^2: Global variables and SimpleHTTP request handler
in thread Global variables and SimpleHTTP request handler

For communication between processes, see perlipc.

Maybe you can use non-blocking communication within a single process by using AnyEvent, but it's likely much easier to write the commands to a file or to simply not return a response from your webserver until the command has been processed, thus pushing the requirement for asynchronicity to Scratch.

For some value of "easy", using threads can make it easy to have shared access to a common data structure which is modified concurrently. If your Perl has been compiled with thread support, that may give you a nicer approach. I still recommend a command queue approach using Thread::Queue.

  • Comment on Re^3: Global variables and SimpleHTTP request handler

Replies are listed 'Best First'.
Re^4: Global variables and SimpleHTTP request handler
by Anonymous Monk on Jan 03, 2017 at 21:39 UTC

    Thank you so much, Thread::Queue does exactly what I need, though the MCE suggestion looks like it would have also worked.

    There was a slight oddity that it did not work with SimpleHTTP backgrounded. I had to create my own thread for that and use run().

    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 qu +eue"); 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); }

    All seems to work nicely. :)