Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Question about communicating with a running perl script

by kcott (Archbishop)
on Jul 08, 2015 at 02:04 UTC ( [id://1133666]=note: print w/replies, xml ) Need Help??


in reply to Question about communicating with a running perl script

G'day Amblikai,

You might consider using Named Pipes for this task.

The basic idea would be to have your SQL (background) script set up the named pipe. It would check if there were any directives (i.e. specific steps to be executed) to be read from the pipe. If so, it would read, and then perform, these in whatever order they arrived (FIFO). If not (or when all piped directives had been exhausted), it would go back to its default list of steps.

Your WEB script would be writing these directives to the named pipe; I assume on a somewhat ad hoc basis. The directives, themselves, could be step numbers or step names; or, potentially, something more complex like commands with options and arguments.

I've put together a couple of test scripts to give you an idea of the techniques involved. There's no DBI or CGI code here; really just ideas for you to modify and adapt to whatever code you currently have. Also, in total, they're quite lengthy, so I've put each in spoiler tags.

First, pm_1133400_sim_sql.pl, which is intended to simulate your SQL (background) script.

Next, pm_1133400_sim_web.pl, simulates your WEB script.

You'll see that both scripts use a PM_1133400_Shared module. That was a convenience for me; you may want something entirely different (e.g. a config file). However, you will, at the very least, need some mechanism that allows both scripts to know the filename of the named pipe (and I recommend you store that information once). Here's PM_1133400_Shared.pm:

Now, if you save those three files in one directory, and make the scripts executable, you can test it like this (to run the SQL script in the background and the WEB script in the foreground):

pm_1133400_sim_sql.pl & pm_1133400_sim_web.pl

The WEB script operates randomly. In some cases, it might start issuing directives before the named pipe is created, so it will put those in a queue and write them to the pipe later:

WEB_SIM: FIFO Unavailable! Queueing step [5]. SQL_SIM: Executing normal step: Step 0 SQL_SIM: Executing normal step: Step 1 WEB_SIM: FIFO Available! Piping from queue [5] ... SQL_SIM: Executing web request: Step 5

Other times, the SQL script may have performed some of its normal steps before any WEB script directive is issued:

SQL_SIM: Executing normal step: Step 0 SQL_SIM: Executing normal step: Step 1 WEB_SIM: FIFO Available! Piping new step [4] SQL_SIM: Executing web request: Step 4 SQL_SIM: Executing normal step: Step 5 SQL_SIM: Executing normal step: Step 6 ...

And, as you can see from that last sample extract, WEB can cause the steps to be performed out-of-order ([0, 1, 4, ...]) but, when those are completed, SQL will continue on in-order ([..., 4, 5, 6]).

I've only used builtin functions, modules and pragmata; you can find documentation for everything via perldoc. Of course, if you have other questions, feel free to ask.

-- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1133666]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-18 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found