[Python] Python writing to file from multiple separate Python processes, do I have to lock...

Discussão em 'Python' iniciado por Stack, Agosto 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python script that is running in a number of different Kubernetes pods at all times (minimum 1, max ~100 at the same time).

    These processes are largely independent of each other, except that at one point, they have to write to the same file (last_appended.txt) in the following fashion:

    with open(filepath, 'w') as file:
    file.write(str(int(time.timestamp())))


    I am wondering if I have to do any sort of locking on this file or if this is such a minimal operation that this is not necessary?

    If I would want to lock the file, I have found the following code to enable this:

    with open(filepath, 'w') as file:
    fcntl.flock(file, fcntl.LOCK_EX)
    file.write(str(int(time.timestamp())))
    fcntl.flock(file, fcntl.LOCK_UN)


    However, I wonder if this is enough to let my processes run smoothly, or that I maybe have to write some sort of try/except loop when a lock is encountered by a process.

    Summarising, my question is two-fold:

    1. Would I have to lock the file at all, or is this such a small operation that it can be done by multiple processes without crashing, and
    2. If no, would my solution to the multiple processes writing to the file be sufficient?

    Continue reading...

Compartilhe esta Página