Python Multiple users append to the same file at t

2019-01-31 12:58发布

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.

4条回答
神经病院院长
2楼-- · 2019-01-31 13:32

Depending on your platform/filesystem location this may not be doable in a safe manner (e.g. NFS). Perhaps you can write to different files and merge the results afterwards?

查看更多
在下西门庆
3楼-- · 2019-01-31 13:36

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.

查看更多
Fickle 薄情
4楼-- · 2019-01-31 13:37

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?

查看更多
相关推荐>>
5楼-- · 2019-01-31 13:57

You didnt state what platform you use, but here is an module you can use that is cross platform: File locking in Python

查看更多
登录 后发表回答