[Tutor] a bit off topic.. - more of a high level arch question!

Danny Yoo dyoo at hashcollision.org
Fri Oct 21 14:23:57 EDT 2016


> Thinking of a situation where I have two "processes" running. They
> each want to operate on a list of files in the dir on a first come
> first operate basis. Once a process finishes with the file, it deletes
> it.
>
> Only one process operates on a file.
>
> I'm curious for ideas/thoughts.


Hi Bruce,

An inter-process locking approach seems straightforward: it allows us
to make sure that only one process is running a particular section of
code at once.  In your case, this allows us to make sure the worker
processes don't get mixed up when deciding what to work on next.

I know that there's some built-in support for file locks in the
standard library, but on brief glance, it looks a bit low-level.

    https://docs.python.org/3.6/library/fcntl.html#fcntl.lockf

... I wonder if there is a nicer API for this.  I've been out of the
loop in terms of what third-party Python libraries are in vogue these
days, but let me do a few searches...

Ok, the following library might be helpful:

    http://fasteners.readthedocs.io/en/latest/
    http://fasteners.readthedocs.io/en/latest/examples.html#interprocess-locks

Does anyone have experience with this "fasteners" library?  It looks
promising!  It appears to provide a nice library for using lock files
for inter-process synchronization; their example of interprocess locks
seems straightforward.


Good luck to you!


More information about the Tutor mailing list