Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Vexing Race Condition

by tybalt89 (Monsignor)
on May 01, 2025 at 15:44 UTC ( [id://11164891]=note: print w/replies, xml ) Need Help??


in reply to Vexing Race Condition

Try this. Path::Tiny::spew writes to a temporary file and then atomic renames to the final name. This should never allow an empty file.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11164885 use warnings; use Path::Tiny; #one.pl my $file = '/dev/shm/tmpcount.txt'; for (1 .. 180) { print "$_\n"; # &filewrite($file,$_); path($file)->spew($_); # NOTE writes to temp file, then atomic renam +es over sleep 1; } #two.pl my $file = '/dev/shm/tmpcount.txt'; my $c; do { if (-e $file) { # $c = &fileread($file); $c = path($file)->slurp; die "C is empty" unless ($c); print "count is $c \n"; } else { print "File not found\n"; } } until ($c == 180);

Replies are listed 'Best First'.
Re^2: Vexing Race Condition
by Maelstrom (Beadle) on May 02, 2025 at 02:17 UTC
    Of all the wheels I regret re-inventing Path::Tiny may be the most regrettable. The atomic renaming seems to have eradicated this race condition entirely. Cheers.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2025-06-21 13:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.