本文整理汇总了Python中gevent.coros.BoundedSemaphore.locked方法的典型用法代码示例。如果您正苦于以下问题:Python BoundedSemaphore.locked方法的具体用法?Python BoundedSemaphore.locked怎么用?Python BoundedSemaphore.locked使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.coros.BoundedSemaphore
的用法示例。
在下文中一共展示了BoundedSemaphore.locked方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: locks
# 需要导入模块: from gevent.coros import BoundedSemaphore [as 别名]
# 或者: from gevent.coros.BoundedSemaphore import locked [as 别名]
class Lock:
""" UNIX-specific exclusive file locks (released when the process ends).
Based on
http://blog.vmfarms.com/2011/03/cross-process-locking-and.html,
adapted for context managers (the 'with' statement).
Modified to be gevent-safe! Locks held by a given Greenlet may not be
taken by other Greenlets until released, _as long as you only create one
Lock object per lockfile_. THIS IS VERY IMPORTANT. *Make sure* that you're
not creating multiple locks on the same file from the same process,
otherwise you'll bypass the gevent lock!
Parameters
----------
f : file or str
File handle or filename to use as the lock.
block : bool
Whether to block or throw IOError if the lock is grabbed multiple
times.
"""
TIMEOUT = 60
def __init__(self, f, block=True):
if isinstance(f, file):
self.filename = f.name
self.handle = f if not f.closed else open(f, 'w')
else:
self.filename = f
mkdirp(os.path.dirname(f))
self.handle = open(f, 'w')
if block:
self.lock_op = fcntl.LOCK_EX
else:
self.lock_op = fcntl.LOCK_EX | fcntl.LOCK_NB
self.block = block
self.gevent_lock = BoundedSemaphore(1)
def acquire(self):
got_gevent_lock = self.gevent_lock.acquire(blocking=self.block)
if not got_gevent_lock:
raise IOError("cannot acquire gevent lock")
fcntl.flock(self.handle, self.lock_op)
def release(self):
fcntl.flock(self.handle, fcntl.LOCK_UN)
self.gevent_lock.release()
def locked(self):
return self.gevent_lock.locked()
def __enter__(self):
self.acquire()
return self
def __exit__(self, type, value, traceback):
self.release()
def __del__(self):
self.handle.close()
示例2: functions
# 需要导入模块: from gevent.coros import BoundedSemaphore [as 别名]
# 或者: from gevent.coros.BoundedSemaphore import locked [as 别名]
#.........这里部分代码省略.........
# get position and set it in the API
if self.cli_args['location']:
position = get_location(self.cli_args['location'])
else:
position = get_location(self.config.location)
self._origPosF = position
if prev_location:
position = prev_location
self.api.set_position(*position)
# retry login every 30 seconds if any errors
self.log.info('Starting Login process...')
login = False
while not login:
login = self.api.login(self.config.auth_service, self.config.username, self.config.get_password())
if not login:
logger.error('Login error, retrying Login in 30 seconds')
self.sleep(30)
self.log.info('Login successful')
self._heartbeat(login, True)
return True
def reload_api(self, prev_location=None):
self.api = None
return self._load_api(prev_location)
'''
Blocking lock
- only locks if current thread (greenlet) doesn't own the lock
- persist=True will ensure the lock will not be released until the user
explicitly sets self.persist_lock=False.
'''
def thread_lock(self, persist=False):
if self.sem.locked():
if self.locker == id(gevent.getcurrent()):
self.log.debug("Locker is -- %s. No need to re-lock", id(gevent.getcurrent()))
return False
else:
self.log.debug("Already locked by %s. Greenlet %s will wait...", self.locker, id(gevent.getcurrent()))
self.sem.acquire()
self.persist_lock = persist
self.locker = id(gevent.getcurrent())
self.log.debug("%s acquired lock (persist=%s)!", self.locker, persist)
return True
'''
Releases the lock if needed and the user didn't persist it
'''
def thread_release(self):
if self.sem.locked() and self.locker == id(gevent.getcurrent()) and not self.persist_lock:
self.log.debug("%s is now releasing lock", id(gevent.getcurrent()))
self.sem.release()
def _callback(self, gt):
try:
if not gt.exception:
result = gt.value
logger.info('Thread finished with result: %s', result)
except KeyboardInterrupt:
return
logger.exception('Error in main loop %s, restarting at location: %s',
gt.exception, self.get_position())
# restart after sleep
self.sleep(30)
self.reload_config()