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


In reply to Re: Question about communicating with a running perl script by kcott
in thread Question about communicating with a running perl script by Amblikai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.