How can I call a perl process that is already running from another script?

Problem: scriptA.cgi sits in an infinite loop and processes an open socket for the Flash client. scriptB.cgi is called from the Internet, does what it needs, and then must tell scriptA to send a message to the client.

Is it possible? I was fixated on how script B identifies an instance of scriptA that sits there with a socket connection, rather than starting one of its own.

all thoughts are appreciated.

+3
source share
5 answers

If communication needs are simple, this is a good signaling app.

, scriptA B - A B .

 # script B
 do_scriptB_job();
 if (open(my $PID_FILE, "<", "scriptA.pid.file")) {
   $process_id_for_scriptA = <$PID_FILE>;
   close $PID_FILE;
   kill 'USR1', $process_id_for_scriptA;  # makes scriptA run the SIGUSR1 handler
 }


 # script A
 open(my $PID_FILE, ">", "scriptA.pid.file");
 print $PID_FILE $$;
 close $PID_FILE;
 my $signaled = 0;
 $SIG{"USR1"} = \sub { $signaled = 1 } # simple SIGUSR1 handler, set a variable
 while ( in_infinite_loop ) {
     if ($signaled) {
         # this block runs only if SIGUSR1 was received 
         # since last time this block was run
         send_a_message_to_the_client();
         $signaled = 0;
     } else {
         do_something_else();
     }
 }
 unlink "scriptA.pid.file";   # cleanup

script A SIGUSR1, script USR1, $signaled. , script .

+5

scriptA pid somwhere ( db - ), scriptB pid db scriptA.

Edit:
,

pid , perls $$ $PID $PROCESS_ID , perl .

. perlvar.

, ID, . , scriptA. (, . )

+2

, PID ( fork() , ... -... , , - ).

, , , perldoc perlipc , :

NAME
    perlipc - Perl interprocess communication (signals, fifos, pipes, safe
    subprocesses, sockets, and semaphores)

DESCRIPTION
    The basic IPC facilities of Perl are built out of the good old Unix
    signals, named pipes, pipe opens, the Berkeley socket routines, and SysV
    IPC calls. Each is used in slightly different situations.
+2

: " " " - IPC ", sqlite ( ), ,

ScriptA.cgi , - "SELECT event FROM events WHERE clientID =?".

ScriptB.cgi simply inserts a row into the event table with the correct client identifier.

This avoids the "find pid" clutter, and also means that you won’t get the IO blocking problems you get with named pipes or if one of the script crashes.

0
source

Source: https://habr.com/ru/post/1726841/


All Articles