Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Multiple users append to the same file at the same time

I'm working on a python script that will be accessed via the web, so there will be multiple users trying to append to the same file at the same time. My worry is that this might cause a race condition where if multiple users wrote to the same file at the same time and it just might corrupt the file.

For example:

#!/usr/bin/env python  g = open("/somepath/somefile.txt", "a") new_entry = "foobar" g.write(new_entry) g.close 

Will I have to use a lockfile for this as this operation looks risky.

like image 623
Ray Y Avatar asked Aug 07 '12 20:08

Ray Y


People also ask

Can multiple processes append to the same file?

Two processes successfully appending to the same file will result in all their bytes in the file in order, but not necessarily contiguously. The caveat is that not all filesystems are POSIX-compatible. Two famous examples are NFS and the Hadoop Distributed File System (HDFS).

Can two Python programs write to the same file?

You mean if suddenly a lot of processes want to write into the same file and hold each other up? @dmeu: That's basically it. If you have multiple processes wanting to write at the same time, only one of them will be able to, and the rest will just sit there waiting instead of doing useful work.


2 Answers

You can use file locking:

import fcntl new_entry = "foobar" with open("/somepath/somefile.txt", "a") as g:     fcntl.flock(g, fcntl.LOCK_EX)     g.write(new_entry)     fcntl.flock(g, fcntl.LOCK_UN) 

Note that on some systems, locking is not needed if you're only writing small buffers, because appends on these systems are atomic.

like image 87
phihag Avatar answered Sep 22 '22 22:09

phihag


If you are doing this operation on Linux, and the cache size is smaller than 4KB, the write operation is atomic and you should be good.

More to read here: Is file append atomic in UNIX?

like image 41
user1767754 Avatar answered Sep 22 '22 22:09

user1767754