本文整理汇总了Python中portalocker.lock方法的典型用法代码示例。如果您正苦于以下问题:Python portalocker.lock方法的具体用法?Python portalocker.lock怎么用?Python portalocker.lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类portalocker
的用法示例。
在下文中一共展示了portalocker.lock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_list
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def init_list(self, data=None):
"""
Initialize asid_list file.
:param data: list or a string
:return:
"""
file_path = os.path.join(running_path, '{sid}_list'.format(sid=self.sid))
if not os.path.exists(file_path):
if isinstance(data, list):
with open(file_path, 'w') as f:
portalocker.lock(f, portalocker.LOCK_EX)
f.write(json.dumps({
'sids': {},
'total_target_num': len(data),
}))
else:
with open(file_path, 'w') as f:
portalocker.lock(f, portalocker.LOCK_EX)
f.write(json.dumps({
'sids': {},
'total_target_num': 1,
}))
示例2: list
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def list(self, data=None):
file_path = os.path.join(running_path, '{sid}_list'.format(sid=self.sid))
if data is None:
with open(file_path, 'r') as f:
portalocker.lock(f, portalocker.LOCK_EX)
result = f.readline()
return json.loads(result)
else:
with open(file_path, 'r+') as f:
portalocker.lock(f, portalocker.LOCK_EX)
result = f.read()
if result == '':
result = {'sids': {}}
else:
result = json.loads(result)
result['sids'][data[0]] = data[1]
f.seek(0)
f.truncate()
f.write(json.dumps(result))
示例3: getLockFilename
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def getLockFilename(self):
"""
Decide the lock filename. If the logfile is file.log, then we use `.__file.lock` and
not `file.log.lock`. This only removes the extension if it's `*.log`.
:return: the path to the lock file.
"""
if self.baseFilename.endswith(".log"):
lock_file = self.baseFilename[:-4]
else:
lock_file = self.baseFilename
lock_file += ".lock"
lock_path, lock_name = os.path.split(lock_file)
# hide the file on Unix and generally from file completion
lock_name = ".__" + lock_name
return os.path.join(lock_path, lock_name)
示例4: _do_lock
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def _do_lock(self):
if self.is_locked:
return # already locked... recursive?
self._open_lockfile()
if self.stream_lock:
for i in range(10):
# noinspection PyBroadException
try:
lock(self.stream_lock, LOCK_EX)
self.is_locked = True
break
except Exception:
continue
else:
raise RuntimeError("Cannot acquire lock after 10 attempts")
else:
self._console_log("No self.stream_lock to lock", stack=True)
示例5: _try_store_in_file
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def _try_store_in_file(self, request, response):
try:
if (not response.session_id or self._forget
or self._unchanged(response)):
# self.clear_session_cookies()
self.save_session_id_cookie()
return False
if response.session_new or not response.session_file:
# Tests if the session sub-folder exists, if not, create it
session_folder = os.path.dirname(response.session_filename)
if not os.path.exists(session_folder): os.mkdir(session_folder)
response.session_file = open(response.session_filename, 'wb')
portalocker.lock(response.session_file, portalocker.LOCK_EX)
response.session_locked = True
if response.session_file:
session_pickled = response.session_pickled or cPickle.dumps(self)
response.session_file.write(session_pickled)
response.session_file.truncate()
finally:
self._close(response)
self.save_session_id_cookie()
return True
示例6: initialize
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def initialize(self):
if self.initialized:
return
else:
self.initialized = True
folder = self.folder
request = self.request
# Lets test if the cache folder exists, if not
# we are going to create it
folder = folder or os.path.join(request.folder, 'cache')
if not os.path.exists(folder):
os.mkdir(folder)
### we need this because of a possible bug in shelve that may
### or may not lock
self.locker_name = os.path.join(folder, 'cache.lock')
self.shelve_name = os.path.join(folder, 'cache.shelve')
示例7: acquire
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def acquire(self):
""" Acquire thread and file locks. Re-opening log for 'degraded' mode.
"""
# handle thread lock
Handler.acquire(self)
# Issue a file lock. (This is inefficient for multiple active threads
# within a single process. But if you're worried about high-performance,
# you probably aren't using this log handler.)
if self.stream_lock:
# If stream_lock=None, then assume close() was called or something
# else weird and ignore all file-level locks.
if self.stream_lock.closed:
# Daemonization can close all open file descriptors, see
# https://bugzilla.redhat.com/show_bug.cgi?id=952929
# Try opening the lock file again. Should we warn() here?!?
try:
self._open_lockfile()
except Exception:
self.handleError(NullLogRecord())
# Don't try to open the stream lock again
self.stream_lock = None
return
lock(self.stream_lock, LOCK_EX)
# Stream will be opened as part by FileHandler.emit()
示例8: release
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def release(self):
""" Release file and thread locks. If in 'degraded' mode, close the
stream to reduce contention until the log files can be rotated. """
try:
if self._rotateFailed:
self._close()
except Exception:
self.handleError(NullLogRecord())
finally:
try:
if self.stream_lock and not self.stream_lock.closed:
unlock(self.stream_lock)
except Exception:
self.handleError(NullLogRecord())
finally:
# release thread lock
Handler.release(self)
示例9: get
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def get(self, count: int=1, readOnly: bool=False, recurlvl=0):
with open(self.FILE, "r+") as file:
portalocker.lock(file, portalocker.LOCK_EX)
ports = []
while len(ports) < count:
file.seek(0)
port = int(file.readline())
if readOnly:
return port
port += 1
if port > self.maxPort:
port = self.minPort
file.seek(0)
file.write(str(port))
try:
checkPortAvailable(("", port))
ports.append(port)
self.logger.debug("new port dispensed: {}".format(port))
except Exception:
if recurlvl < self.maxportretries:
self.logger.debug("port {} unavailable, trying again...".
format(port))
recurlvl += 1
else:
self.logger.debug("port {} unavailable, max retries {} "
"reached".
format(port, self.maxportretries))
raise
return ports
示例10: status
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def status(self, data=None):
file_path = os.path.join(running_path, '{sid}_status'.format(sid=self.sid))
if data is None:
with open(file_path) as f:
portalocker.lock(f, portalocker.LOCK_EX)
result = f.readline()
return json.loads(result)
else:
data = json.dumps(data)
with open(file_path, 'w') as f:
portalocker.lock(f, portalocker.LOCK_EX)
f.writelines(data)
示例11: data
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def data(self, data=None):
file_path = os.path.abspath(running_path + '/{sid}_data'.format(sid=self.sid))
if data is None:
with open(file_path) as f:
portalocker.lock(f, portalocker.LOCK_EX)
result = f.readline()
return json.loads(result)
else:
data = json.dumps(data, sort_keys=True)
with open(file_path, 'w+') as f:
portalocker.lock(f, portalocker.LOCK_EX)
f.writelines(data)
示例12: emit
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def emit(self, record):
"""
Emit a record.
Override from parent class to handle file locking for the duration of rollover and write.
This also does the formatting *before* locks are obtained, in case the format itself does
logging calls from within. Rollover also occurs while the lock is held.
"""
# noinspection PyBroadException
try:
msg = self.format(record)
try:
self._do_lock()
try:
if self.shouldRollover(record):
self.doRollover()
except Exception as e:
self._console_log("Unable to do rollover: %s" % (e,), stack=True)
# Continue on anyway
self.do_write(msg)
finally:
self._do_unlock()
except (KeyboardInterrupt, SystemExit):
raise
except Exception:
self.handleError(record)
示例13: lock
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def lock(file, flags):
hfile = win32file._get_osfhandle(_getfd(file))
try:
win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped)
except pywintypes.error, exc_value:
# error: (33, 'LockFileEx', 'The process cannot access the file because another process has locked a portion of the file.')
if exc_value[0] == 33:
raise LockException(LockException.LOCK_FAILED, exc_value[2])
else:
# Q: Are there exceptions/codes we should be dealing with here?
raise
示例14: acquire
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def acquire(self):
""" Acquire thread and file locks. Also re-opening log file when running
in 'degraded' mode. """
# handle thread lock
Handler.acquire(self)
lock(self.stream_lock, LOCK_EX)
if self.stream.closed:
self._openFile(self.mode)
示例15: release
# 需要导入模块: import portalocker [as 别名]
# 或者: from portalocker import lock [as 别名]
def release(self):
""" Release file and thread locks. Flush stream and take care of closing
stream in 'degraded' mode. """
try:
if self.stream.closed==False:
self.stream.flush()
if self._rotateFailed:
self.stream.close()
finally:
try:
unlock(self.stream_lock)
finally:
# release thread lock
Handler.release(self)