The way which immediately springs to mind (and of course, TMTOWTDI) is to have your script fork into two processes, one of which creates the files, and the other one of which produces them.

You'll need a way of signalling that a particular file is finished to the FTP script, so it doesn't try to upload a half-finished file. Provided that you're not dealing with an NFS filesystem, then flock can work nicely here, resulting in the consumer process efficiently blocking until it has its turn to read the file. There is a potentially rare race condition where the consumer process could lock the file before the producer process has had a chance to lock it first, so it's probably wise for the consumer to never attempt to lock an empty file. This assumes that an empty file is never valid. If an empty file is valid, then you could use file permission bits to indicate when a file is ready for uploading.

You could use shared memory to pass files (or information about files) about, which is a very good solution, as the consumer process can just wait for a message to go ahead with the next file. You could replace shared memory with unix domain sockets here if you felt more comfortable with them.

If fork() gives you the willies, then you could just split your script into two and make sure that one is always started with the other (a small shell script can help here). This means you can't do shared memory, but if you're not comfortable with fork, then the chances are you're not comfortable with shared memory either.

Hope that this helps you get started.

Cheers
Paul


In reply to Re: FTP in background by pjf
in thread FTP in background by Anonymous Monk

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.