Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I am currently in a situation where multiple clients need to make data available to one server application. Both the server and client are running on the same machine, so this makes it very easy to handle. My question is: which way is better? I've thought of having the clients append to a flat text file, while the server uses tail -f to get at the data. My second thought was using a database. Here is example structure of my thoughts. First, the database method:

################## # Server my $id = 0; while (1) { # do mysql query: "SELECT FROM msgs WHERE id>?", $id for my $msg ($results_of_mysql_query) { # do something with this line of data } sleep 1; } ################## # Client: while (<STDIN>) { chomp; # do mysql insert: "INSERT INTO msgs VALUES(?,?)", '', $_; }

Then let's take a look at the flat text file way of getting this done:

################## # Server: open my $msgs, "| tail -f data.txt" or die "ouch: $!"; while (<$msgs>) { # do something with this line of data } ################## # Client: use Fcntl ':flock'; while (<STDIN>) { chomp; open my $msgs, '>>', 'data.txt' or die "ouch: $!"; flock $msgs, LOCK_EX; print( $msgs, ($_ . "\n") ); close $msgs; }

So out of these two methods, which one strikes you as being more efficient? Or can you think of a different method altogether that beats the pants of both of these? As well, is there any flaw in my logic of the file locking for the flat file method? Will `tail` still be capable of reading the appended data, even if I take an exclusive lock on the file while appending is done? Thanks :)


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.


In reply to Database vs. tail -f by Coruscate

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found