
sleuth
Enthusiast
Nov 5, 2000, 7:52 PM
Post #7 of 9
(2964 views)
|
|
Re: prevent concurrent writing to a text file
[In reply to]
|
Can't Post
|
|
You could probably complain, he he This is also a good way. Put these two subs at the top of your script, <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> sub Lock { local ($lockname) = shift; local ($endtime); $endtime = time + 10; while ((-e "$lockname") && (time < $endtime)) { sleep(1); } open (LOCKFILE, ">$lockname"); } sub Unlock { local ($lockname) = shift; close (LOCKFILE); unlink ("$lockname"); } </pre><HR></BLOCKQUOTE> And when you want to open a file do this. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> &Lock("lock.file"); open(db, ">file.db"); print db "bla"; close(db); &Unlock("lock.file"); </pre><HR></BLOCKQUOTE> lock.file is the name of the lock file you will be creating. As long as the file doesn't exist in the directory that your script is in, your ok with that name. Sleuth
|