/usr/lib/python3/dist-packages/flufl/lock/tests/subproc.py is in python3-flufl.lock 2.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | # Copyright (C) 2004-2012 by Barry A. Warsaw
#
# This file is part of flufl.lock.
#
# flufl.lock is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# flufl.lock is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with flufl.lock. If not, see <http://www.gnu.org/licenses/>.
"""A helper for setting up subprocess locks."""
from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
'_acquire',
'_waitfor',
]
import time
import multiprocessing
from flufl.lock import Lock
def child_locker(filename, lifetime, queue):
# First, acquire the file lock.
with Lock(filename, lifetime):
# Now inform the parent that we've acquired the lock.
queue.put(True)
# Keep the file lock for a while.
time.sleep(lifetime.seconds - 1)
def _acquire(filename, lifetime=None):
"""Acquire the named lock file in a subprocess."""
queue = multiprocessing.Queue()
proc = multiprocessing.Process(target=child_locker,
args=(filename, lifetime, queue))
proc.start()
while not queue.get():
time.sleep(0.1)
def child_waitfor(filename, lifetime, queue):
t0 = time.time()
# Try to acquire the lock.
with Lock(filename, lifetime):
# Tell the parent how long it took to acquire the lock.
queue.put(time.time() - t0)
def _waitfor(filename, lifetime):
"""Fire off a child that waits for a lock."""
queue = multiprocessing.Queue()
proc = multiprocessing.Process(target=child_waitfor,
args=(filename, lifetime, queue))
proc.start()
time = queue.get()
return time
|