def get_content_all_messages(self):
"""
Get all messages
"""
try:
buffers = list()
with open(self.file_path, 'r') as fd:
fcntl.flock(fd, fcntl.LOCK_SH)
for row_msg in fd.readlines():
try:
msg = self.parse_row_message(row_msg)
buffers.append(json.loads(msg.content))
except Exception:
pass
fcntl.flock(fd, fcntl.LOCK_UN)
return buffers
except Exception:
return
python类LOCK_UN的实例源码
def push(self, message):
""" Push a new message. """
if self.overflow_mode == 'drop':
if self.max_length > -1 and self.get_length() >= self.max_length:
return
if self.max_size > -1 and self.get_size() >= self.max_size:
return
with open(self.file_path, 'a') as fd:
# Let's hold an exclusive lock.
fcntl.flock(fd, fcntl.LOCK_EX)
fd.write(message.serialize())
fcntl.flock(fd, fcntl.LOCK_UN)
fd.close()
if self.overflow_mode == 'slide':
if self.max_size == -1 and self.max_length > -1:
while self.get_length() > self.max_length:
self.shift()
elif self.max_size > -1 and self.max_length == -1:
while self.get_size() > self.max_size:
self.shift()
def lock_path(path, timeout=0):
fd = os.open(path, os.O_CREAT)
flags = fcntl.fcntl(fd, fcntl.F_GETFD, 0)
flags |= fcntl.FD_CLOEXEC
fcntl.fcntl(fd, fcntl.F_SETFD, flags)
started = time.time()
while True:
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
if started < time.time() - timeout:
raise LockError("Couldn't obtain lock")
else:
break
time.sleep(0.1)
def unlock_path():
fcntl.flock(fd, fcntl.LOCK_UN)
os.close(fd)
return unlock_path
def unlock(self,fname):
#TODO: something more clever here?
lock_name=fname+'.lock'
try:
f=self._locks[lock_name]
if f is not None:
fcntl.lockf(f.fileno(), fcntl.LOCK_UN)
f.close()
del self._locks[lock_name]
# try:
# os.unlink(lock_name)
# except OSError:
#probably somebody else is blocking
# pass
except KeyError:
pass
#def __del__(self):
#self.do_cleanup()
# pass
def release(self):
'''
Release a previously acquired lock.
'''
assert self._locked
assert self._lock_file
# Note that this actually leaves the lock file around, but deleting it without a race
# is not trivial.
fcntl.flock(self._lock_file, fcntl.LOCK_UN)
verbose('Lock "%s" released.' % self._lock_file_path)
self._locked = False
self._lock_file.close()
self._lock_file = None
def write_data(self):
if self.config_newer_than_data():
self.read_data()
return False
else:
try:
self.data = {"clouds":self.quads.clouds.data, "hosts":self.quads.hosts.data, "history":self.quads.history.data, "cloud_history":self.quads.cloud_history.data}
with open(self.config, 'w') as yaml_file:
fcntl.flock(yaml_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
yaml_file.write(yaml.dump(self.data, default_flow_style=False))
fcntl.flock(yaml_file, fcntl.LOCK_UN)
self.read_data()
return True
except Exception, ex:
self.logger.error("There was a problem with your file %s" % ex)
return False
def config_cache_lock():
"""
Obtain the config cache lock, perform the code
in the context and then let the lock go.
:yield None
:return None:
"""
with open(conf().CONFIG_CACHE_LOCK, 'w+') as lock:
try:
log.debug(
"Getting config cache lock. "
"If this blocks forever, try deleting file "
"{} and restart the process.".format(conf().CONFIG_CACHE_LOCK)
)
flock(lock, LOCK_EX) # Blocks until lock becomes available
yield
finally:
log.debug("Releasing the config cache lock")
flock(lock, LOCK_UN)
def __del__(self):
import sys
import os
if not self.initialized:
return
try:
if sys.platform == 'win32':
if hasattr(self, 'fd'):
os.close(self.fd)
os.unlink(self.lockfile)
else:
import fcntl
fcntl.lockf(self.fp, fcntl.LOCK_UN)
# os.close(self.fp)
if os.path.isfile(self.lockfile):
os.unlink(self.lockfile)
except Exception as e:
raise
def flock(path):
"""Attempt to acquire a POSIX file lock.
"""
with open(path, "w+") as lf:
try:
fcntl.flock(lf, fcntl.LOCK_EX | fcntl.LOCK_NB)
acquired = True
yield acquired
except OSError:
acquired = False
yield acquired
finally:
if acquired:
fcntl.flock(lf, fcntl.LOCK_UN)
def open_with_lock(filename, mode):
with open(filename, mode) as f:
while True:
try:
fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
yield f
break
except IOError as e:
# raise on unrelated IOErrors
if e.errno != errno.EAGAIN:
raise
else:
time.sleep(0.1)
try:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
except Exception:
pass
def unlock(self):
"""Unlocks the LockFile."""
if self._fileobj:
# To avoid race conditions with the next caller
# waiting for the lock file, it is simply
# truncated instead of removed.
try:
fcntl.lockf(self._fileobj, fcntl.LOCK_UN)
self._fileobj.truncate(0)
self._fileobj.close()
self._lock.release()
except EnvironmentError:
# If fcntl, or the file operations returned
# an exception, drop the lock. Do not catch
# the exception that could escape from
# releasing the lock.
self._lock.release()
raise
finally:
self._fileobj = None
else:
if self._provide_mutex:
assert not self._lock.locked
def updateDB(file_db, msg):
db = init_DB(file_db)
if not db:
data = msg
else:
namelist = db['??'].split(",")
if msg['??'] not in namelist:
namelist.append(msg['??'])
db['??'] = ",".join(namelist)
try:
db['eventid'] = db['eventid'] + "," + msg['eventid']
except:
pass
# if db['??'] != msg['??']:
# db['??'] = db['??'] + "," + msg['??']
if "IP" in msg and "IP" in db:
if db['IP'] != msg['IP']:
db['IP'] = db['IP'] + "," + msg['IP']
db['??'] = db['??'] + msg['??']
data = db
with open(file_db, 'w') as f:
fcntl.flock(f, fcntl.LOCK_EX)
json.dump(data, f, ensure_ascii=False)
fcntl.flock(f, fcntl.LOCK_UN)
def display(self, image=None):
flock(self.mutex, LOCK_EX)
content = self.readRecordingCount()
if len(content) == 2 and self.isTakingFrames(content[0]):
self.decrementRecordingCount(content[0],self.isStoringWithBg(content[1]))
if self.isStoringWithBg(content[1]): self.save(includeBg=True)
else: self.save(includeBg=False)
if image is None: image = self.buffer
self.set_window()
pixelbytes = list(Adafruit_ILI9341.image_to_data(image))
self.data(pixelbytes)
if self.store and self.isTakingFrames(self.readTouchRecordingCount()[0]):
image = Image.new('RGBA',(568, 428))
draw = ImageDraw.Draw(image)
draw.ellipse((122+320-self.y, 12+self.x, 152+320-self.y, 42+self.x), fill = 'red', outline ='red')
self.save(img=image)
self.store = False
self.x, self.y = -1, -1
flock(self.mutex, LOCK_UN)
def update(path):
"""
allow concurrent update of metadata
"""
p = os.path.join(path, "metadata.json")
# we have to open writeable to get a lock
with open(p, "a") as f:
fcntl.lockf(f, fcntl.LOCK_EX)
data = load(path)
yield(data)
save(path, data)
fcntl.lockf(f, fcntl.LOCK_UN)
def _unlock_file(f):
"""Unlock file f using lockf and dot locking."""
if fcntl:
fcntl.lockf(f, fcntl.LOCK_UN)
if os.path.exists(f.name + '.lock'):
os.remove(f.name + '.lock')
def unlock_and_close(self):
"""Close and unlock the file using the fcntl.lockf primitive."""
if self._locked:
fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN)
self._locked = False
if self._fh:
self._fh.close()
def main():
exit_status = 1
try:
args = parse_args()
# Make sure the exporter is only running once.
lock_file = '/var/lock/{}.lock'.format(os.path.basename(sys.argv[0]))
lock_fd = os.open(lock_file, os.O_CREAT)
lock_success = False
try:
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
lock_success = True
except IOError:
msg = 'Failed to export metrics, another instance is running.'
syslog.syslog(syslog.LOG_INFO, msg)
sys.stderr.write(msg + '\n')
if lock_success:
# Create a new registry, otherwise unwanted default collectors are
# added automatically.
registry = prometheus_client.CollectorRegistry()
# Register our own collector and write metrics to STDOUT.
registry.register(CephRgwCollector(**vars(args)))
sys.stdout.write(prometheus_client.generate_latest(registry))
sys.stdout.flush()
# Unlock the lock file.
fcntl.flock(lock_fd, fcntl.LOCK_UN)
exit_status = 0
except Exception as e:
syslog.syslog(syslog.LOG_ERR, str(e))
# Cleanup
os.close(lock_fd)
if lock_success:
try:
os.unlink(lock_file)
except:
pass
sys.exit(exit_status)
def clean_up(self):
# this is not really needed
try:
if self.fh is not None:
if OS_WIN:
os.close(self.fh)
os.unlink(LOCK_PATH)
else:
fcntl.lockf(self.fh, fcntl.LOCK_UN)
self.fh.close() # ???
os.unlink(LOCK_PATH)
except Exception as err:
# logger.exception(err)
raise # for debugging porpuses, do not raise it on production
def lockfile(file):
"flock a given file, then unflock it immediately"
if _lockonly(file):
flock(file, LOCK_UN)
# Options
def __init__(self, mutex_name):
check_valid_mutex_name(mutex_name)
filename = os.path.join(tempfile.gettempdir(), mutex_name)
try:
handle = open(filename, 'w')
fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
except:
self._release_mutex = NULL
self._acquired = False
try:
handle.close()
except:
pass
else:
def release_mutex(*args, **kwargs):
# Note: can't use self here!
if not getattr(release_mutex, 'called', False):
release_mutex.called = True
try:
fcntl.flock(handle, fcntl.LOCK_UN)
except:
traceback.print_exc()
try:
handle.close()
except:
traceback.print_exc()
try:
# Removing is pretty much optional (but let's do it to keep the
# filesystem cleaner).
os.unlink(filename)
except:
pass
# Don't use __del__: this approach doesn't have as many pitfalls.
self._ref = weakref.ref(self, release_mutex)
self._release_mutex = release_mutex
self._acquired = True
def unlock_and_close(self):
"""Close and unlock the file using the fcntl.lockf primitive."""
if self._locked:
fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN)
self._locked = False
if self._fh:
self._fh.close()
def close(self):
self.save()
self.storage.close()
if self.lock:
fcntl.lockf(self.lock, fcntl.LOCK_UN)
self.lock.close()
os.unlink(self.lock.name)
self.opened = False
def unlock(self):
if self.lockfp is None:
return
fd = self.lockfp.fileno()
fcntl.lockf(fd, fcntl.LOCK_UN)
self.lockfp.close()
self.lockfp = None
def unlock_file(fileobj):
fcntl.flock(fileobj, fcntl.LOCK_UN)
def lockfile(filename):
with open(filename, "wb") as opened:
fd = opened.fileno()
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError as ioe:
if ioe.errno not in (errno.EACCES, errno.EAGAIN):
raise
yield False
else:
try:
yield True
finally:
fcntl.flock(fd, fcntl.LOCK_UN)
def unlock(f):
ret = fcntl.flock(_fd(f), fcntl.LOCK_UN)
return (ret == 0)
def unlock(f):
ret = fcntl.flock(_fd(f), fcntl.LOCK_UN)
return (ret == 0)
def _unlock_file(f):
"""Unlock file f using lockf and dot locking."""
if fcntl:
fcntl.lockf(f, fcntl.LOCK_UN)
if os.path.exists(f.name + '.lock'):
os.remove(f.name + '.lock')
def unlock(self):
fcntl.lockf(self.lockfile, fcntl.LOCK_UN)
def __exit__(self, exc_type, exc_value, traceback):
if self.fh is not None:
fcntl.lockf(self.fh, fcntl.LOCK_UN)
self.fh.close()
os.unlink(LOCK_PATH)