本文整理汇总了Python中twisted.python.lockfile.FilesystemLock.unlock方法的典型用法代码示例。如果您正苦于以下问题:Python FilesystemLock.unlock方法的具体用法?Python FilesystemLock.unlock怎么用?Python FilesystemLock.unlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.lockfile.FilesystemLock
的用法示例。
在下文中一共展示了FilesystemLock.unlock方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: copyPackage
# 需要导入模块: from twisted.python.lockfile import FilesystemLock [as 别名]
# 或者: from twisted.python.lockfile.FilesystemLock import unlock [as 别名]
def copyPackage(title):
"""
Copy package directory to db path using a file lock to avoid potential
concurrency race conditions.
@param title: string to use in log entry
@type title: C{str}
"""
dbpath = FilePath(TimezoneCache.getDBPath())
pkgpath = TimezoneCache.FilteredFilePath(TimezoneCache._getPackageDBPath())
lockfile = FilesystemLock(dbpath.path + ".lock")
result = lockfile.lock()
try:
if result and not dbpath.exists():
log.info(
"{title} timezones from {pkg} to {to}",
title=title,
pkg=pkgpath.path,
to=dbpath.path
)
# Copy over the entire package
pkgpath.copyFilteredDirectoryTo(dbpath)
finally:
if result:
lockfile.unlock()
示例2: do_restore
# 需要导入模块: from twisted.python.lockfile import FilesystemLock [as 别名]
# 或者: from twisted.python.lockfile.FilesystemLock import unlock [as 别名]
def do_restore():
lock = FilesystemLock("/var/run/jolicloud_restore_utility.lock")
if lock.lock():
if os.environ.get('DISPLAY', False):
JolicloudRestoreUtilityGtk()
else:
JolicloudRestoreUtilityText()
reactor.run()
lock.unlock()
示例3: OptionParser
# 需要导入模块: from twisted.python.lockfile import FilesystemLock [as 别名]
# 或者: from twisted.python.lockfile.FilesystemLock import unlock [as 别名]
log.error('Sendchange failed for %s: ' % release, exc_info=True)
if rc != 0:
sys.exit(rc)
if __name__ == '__main__':
parser = OptionParser(__doc__)
parser.add_option('-l', '--lockfile', dest='lockfile',
default=path.join(os.getcwd(), ".release-runner.lock"))
parser.add_option('-c', '--config', dest='config',
help='Configuration file')
options = parser.parse_args()[0]
if not options.config:
parser.error('Need to pass a config')
lockfile = options.lockfile
log.debug("Using lock file %s", lockfile)
lock = FilesystemLock(lockfile)
if not lock.lock():
raise Exception("Cannot acquire lock: %s" % lockfile)
log.debug("Lock acquired: %s", lockfile)
if not lock.clean:
log.warning("Previous run did not properly exit")
try:
main(options)
finally:
log.debug("Releasing lock: %s", lockfile)
lock.unlock()
示例4: LockResource
# 需要导入模块: from twisted.python.lockfile import FilesystemLock [as 别名]
# 或者: from twisted.python.lockfile.FilesystemLock import unlock [as 别名]
class LockResource(object):
"""
Handle requests for locking documents.
This class uses Twisted's Filesystem lock to manage a lock in the shared
database.
"""
url_pattern = '/%s/lock/{uuid}' % SHARED_DB_NAME
"""
"""
TIMEOUT = 300 # XXX is 5 minutes reasonable?
"""
The timeout after which the lock expires.
"""
# used for lock doc storage
TIMESTAMP_KEY = '_timestamp'
LOCK_TOKEN_KEY = '_token'
FILESYSTEM_LOCK_TRIES = 5
FILESYSTEM_LOCK_SLEEP_SECONDS = 1
def __init__(self, uuid, state, responder):
"""
Initialize the lock resource. Parameters to this constructor are
automatically passed by u1db.
:param uuid: The user unique id.
:type uuid: str
:param state: The backend database state.
:type state: u1db.remote.ServerState
:param responder: The infrastructure to send responses to client.
:type responder: u1db.remote.HTTPResponder
"""
self._shared_db = state.open_database(SHARED_DB_NAME)
self._lock_doc_id = '%s%s' % (SHARED_DB_LOCK_DOC_ID_PREFIX, uuid)
self._lock = FilesystemLock(
os.path.join(
tempfile.gettempdir(),
hashlib.sha512(self._lock_doc_id).hexdigest()))
self._state = state
self._responder = responder
@http_app.http_method(content=str)
def put(self, content=None):
"""
Handle a PUT request to the lock document.
A lock is a document in the shared db with doc_id equal to
'lock-<uuid>' and the timestamp of its creation as content. This
method obtains a threaded-lock and creates a lock document if it does
not exist or if it has expired.
It returns '201 Created' and a pair containing a token to unlock and
the lock timeout, or '403 AlreadyLockedError' and the remaining amount
of seconds the lock will still be valid.
:param content: The content of the PUT request. It is only here
because PUT requests with empty content are considered
invalid requests by u1db.
:type content: str
"""
# obtain filesystem lock
if not self._try_obtain_filesystem_lock():
self._responder.send_response_json(
LockTimedOutError.status, # error: request timeout
error=LockTimedOutError.wire_description)
return
created_lock = False
now = time.time()
token = hashlib.sha256(os.urandom(10)).hexdigest() # for releasing
lock_doc = self._shared_db.get_doc(self._lock_doc_id)
remaining = self._remaining(lock_doc, now)
# if there's no lock, create one
if lock_doc is None:
lock_doc = self._shared_db.create_doc(
{
self.TIMESTAMP_KEY: now,
self.LOCK_TOKEN_KEY: token,
},
doc_id=self._lock_doc_id)
created_lock = True
else:
if remaining == 0:
# lock expired, create new one
lock_doc.content = {
self.TIMESTAMP_KEY: now,
self.LOCK_TOKEN_KEY: token,
}
self._shared_db.put_doc(lock_doc)
created_lock = True
self._try_release_filesystem_lock()
# send response to client
if created_lock is True:
#.........这里部分代码省略.........