本文整理汇总了Python中gevent.coros.BoundedSemaphore类的典型用法代码示例。如果您正苦于以下问题:Python BoundedSemaphore类的具体用法?Python BoundedSemaphore怎么用?Python BoundedSemaphore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BoundedSemaphore类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: locks
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: LiberateProtocol
class LiberateProtocol(protocols.BaseProtocol):
"""协议"""
buff = ""
def connectionMade(self):
"""连接建立处理
"""
address = self.transport.getAddress()
logger.info('Client %d login in.[%s,%d]' % (self.transport.sessionno,
address[0], address[1]))
self.factory.connmanager.addConnection(self)
self.factory.doConnectionMade(self)
self.sem = BoundedSemaphore(1)
def connectionLost(self, reason):
"""连接断开处理
"""
logger.info('Client %d login out(%s).' % (self.transport.sessionno,
reason))
self.factory.doConnectionLost(self)
self.factory.connmanager.dropConnectionByID(self.transport.sessionno)
def safeToWriteData(self, data, command):
"""线程安全的向客户端发送数据
@param data: str 要向客户端写的数据
"""
if data is None:
return
senddata = self.factory.produceResult(data, command)
self.sem.acquire()
self.transport.sendall(senddata)
self.sem.release()
def dataReceived(self, data):
"""数据到达处理
@param data: str 客户端传送过来的数据
"""
length = self.factory.dataprotocl.getHeadlength() # 获取协议头的长度
self.buff += data
while self.buff.__len__() >= length:
unpackdata = self.factory.dataprotocl.unpack(self.buff[:length])
if not unpackdata.get('result'):
logger.info('illegal data package --')
self.connectionLost('illegal data package')
break
command = unpackdata.get('command')
rlength = unpackdata.get('length')
request = self.buff[length:length + rlength]
if request.__len__() < rlength:
logger.info('some data lose')
break
self.buff = self.buff[length + rlength:]
response = self.factory.doDataReceived(self, command, request)
# if not response:
# continue
self.safeToWriteData(response, command)
示例3: __init__
def __init__(self, redis_uve_server, logger):
self._local_redis_uve = redis_uve_server
self._redis_uve_list = []
self._logger = logger
self._sem = BoundedSemaphore(1)
self._redis = None
if self._local_redis_uve:
self._redis = redis.StrictRedis(self._local_redis_uve[0],
self._local_redis_uve[1], db=1)
示例4: connectionMade
def connectionMade(self):
"""连接建立处理
"""
address = self.transport.getAddress()
logger.info('Client %d login in.[%s,%d]' % (self.transport.sessionno,
address[0], address[1]))
self.factory.connmanager.addConnection(self)
self.factory.doConnectionMade(self)
self.sem = BoundedSemaphore(1)
示例5: __init__
def __init__(self, size, exc_classes=DEFAULT_EXC_CLASSES, keepalive=None):
self.size = size
self.conn = deque()
self.lock = BoundedSemaphore(size)
self.keepalive = keepalive
# Exceptions list must be in tuple form to be caught properly
self.exc_classes = tuple(exc_classes)
for i in xrange(size):
self.lock.acquire()
for i in xrange(size):
gevent.spawn_later(self.SPAWN_FREQUENCY*i, self._addOne)
if self.keepalive:
gevent.spawn(self._keepalive_periodic)
示例6: __init__
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)
示例7: __init__
def __init__(self, args):
self.thread = None
self.socket = None
self.cli_args = args
self.force_debug = args['debug']
# timers, counters and triggers
self.pokemon_caught = 0
self._error_counter = 0
self._error_threshold = 10
self.start_time = time()
self.exp_start = None
self._heartbeat_number = 1 # setting this back to one because we make parse a full heartbeat during login!
self._heartbeat_frequency = 3 # 1 = always
self._full_heartbeat_frequency = 15 # 10 = as before (every 10th heartbeat)
self._farm_mode_triggered = False
# objects, order is important!
self.config = None
self._load_config()
self.log = create_logger(__name__, self.config.log_colors["poketrainer".upper()])
self._open_socket()
self.player = Player({})
self.player_stats = PlayerStats({})
self.inventory = Inventory(self, [])
self.fort_walker = FortWalker(self)
self.map_objects = MapObjects(self)
self.poke_catcher = PokeCatcher(self)
self.incubate = Incubate(self)
self.evolve = Evolve(self)
self.release = Release(self)
self.sniper = Sniper(self)
self._origPosF = (0, 0, 0)
self.api = None
self._load_api()
# config values that might be changed during runtime
self.step_size = self.config.step_size
self.should_catch_pokemon = self.config.should_catch_pokemon
# threading / locking
self.sem = BoundedSemaphore(1) # gevent
self.persist_lock = False
self.locker = None
示例8: __init__
def __init__(self, redis_uve_server, logger, redis_password=None):
self._local_redis_uve = redis_uve_server
self._redis_uve_list = []
self._logger = logger
self._sem = BoundedSemaphore(1)
self._redis = None
self._redis_password = redis_password
if self._local_redis_uve:
self._redis = redis.StrictRedis(self._local_redis_uve[0],
self._local_redis_uve[1],
password=self._redis_password,
db=1)
self._uve_reverse_map = {}
for h,m in UVE_MAP.iteritems():
self._uve_reverse_map[m] = h
示例9: __init__
def __init__(self, discServer, zk_srv_ip='127.0.0.1',
zk_srv_port='2181', reset_config=False):
self._reset_config = reset_config
self._service_id_to_type = {}
self._ds = discServer
self._zk_sem = BoundedSemaphore(1)
self._election = None
self._restarting = False
zk_endpts = []
for ip in zk_srv_ip.split(','):
zk_endpts.append('%s:%s' %(ip, zk_srv_port))
# logging
logger = logging.getLogger('discovery-service')
logger.setLevel(logging.WARNING)
handler = logging.handlers.RotatingFileHandler('/var/log/contrail/discovery_zk.log', maxBytes=1024*1024, backupCount=10)
log_format = logging.Formatter('%(asctime)s [%(name)s]: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
handler.setFormatter(log_format)
logger.addHandler(handler)
self._zk = kazoo.client.KazooClient(
hosts=','.join(zk_endpts),
handler=kazoo.handlers.gevent.SequentialGeventHandler(),
logger=logger)
self._logger = logger
# connect
self.connect()
if reset_config:
self.delete_node("/services", recursive=True)
self.delete_node("/clients", recursive=True)
self.delete_node("/election", recursive=True)
# create default paths
self.create_node("/services")
self.create_node("/clients")
self.create_node("/election")
self._debug = {
'subscription_expires': 0,
'oos_delete': 0,
'db_excepts': 0,
}
示例10: __init__
def __init__(self, discServer, zk_srv_ip='127.0.0.1',
zk_srv_port='2181', reset_config=False):
self._reset_config = reset_config
self._service_id_to_type = {}
self._ds = discServer
self._zk_sem = BoundedSemaphore(1)
zk_endpts = []
for ip in zk_srv_ip.split(','):
zk_endpts.append('%s:%s' %(ip, zk_srv_port))
self._zk = kazoo.client.KazooClient(
hosts=','.join(zk_endpts), timeout=120,
handler=kazoo.handlers.gevent.SequentialGeventHandler())
# connect
while True:
try:
self._zk.start()
break
except gevent.event.Timeout as e:
self.syslog(
'Failed to connect with Zookeeper -will retry in a second')
gevent.sleep(1)
# Zookeeper is also throwing exception due to delay in master election
except Exception as e:
self.syslog('%s -will retry in a second' % (str(e)))
gevent.sleep(1)
self.syslog('Connected to ZooKeeper!')
if reset_config:
self._zk.delete("/services", recursive=True)
self._zk.delete("/clients", recursive=True)
self._zk.delete("/election", recursive=True)
# create default paths
self.create_node("/services")
self.create_node("/clients")
self.create_node("/election")
self._debug = {
'subscription_expires': 0,
'oos_delete': 0,
'db_excepts': 0,
}
示例11: load
def load(self, spider):
redis_args = dict(host=self.settings.REDIS_URL,
port=self.settings.REDIS_PORT,
db=self.settings.REDIS_DB)
if hasattr(self.settings, 'NAMESPACE'):
redis_args['namespace'] = self.settings.NAMESPACE
else:
redis_args['namespace'] = spider.name
self.url_set = redisds.Set('urlset', **redis_args)
self.url_queue = redisds.Queue('urlqueue', serializer=Pickle(),
**redis_args)
self.runner = redisds.Lock("runner:%s" % uuid4().hex, **redis_args)
self.runners = redisds.Dict("runner:*", **redis_args)
self.stats = redisds.Dict("stats:*", **redis_args)
self.lock = BoundedSemaphore(1)
self.running_count = 0
self.allowed_urls_regex = self.get_regex(spider.allowed_domains)
self.spider = spider
self.start()
示例12: __init__
def __init__(self, local_ip, local_port, redis_sentinel_client, service):
self._redis_sentinel_client = redis_sentinel_client
self._local_ip = local_ip
self._local_port = int(local_port)
self._service = service
self._uve_server_task = None
self._redis = None
self._redis_master_info = None
self._master_last_updated = None
self._num_mastership_changes = 0
self._sem = BoundedSemaphore(1)
if redis_sentinel_client is not None:
self._redis_master_info = \
redis_sentinel_client.get_redis_master(service)
if self._redis_master_info is not None:
self._num_mastership_changes += 1
self._master_last_updated = UTCTimestampUsec()
self._redis = redis.StrictRedis(self._redis_master_info[0],
self._redis_master_info[1],
db=0)
self._uve_server_task = gevent.spawn(self.run)
示例13: __init__
def __init__(self, discServer, zk_srv_ip='127.0.0.1',
zk_srv_port='2181', reset_config=False):
self._reset_config = reset_config
self._service_id_to_type = {}
self._ds = discServer
self._zk_sem = BoundedSemaphore(1)
self._zk = kazoo.client.KazooClient(
hosts='%s:%s' % (zk_srv_ip, zk_srv_port), timeout=120,
handler=kazoo.handlers.gevent.SequentialGeventHandler())
# connect
while True:
try:
self._zk.start()
break
except gevent.event.Timeout as e:
self.syslog(
'Failed to connect with Zookeeper -will retry in a second')
gevent.sleep(1)
self.syslog('Connected to ZooKeeper!')
if reset_config:
self._zk.delete("/services", recursive=True)
self._zk.delete("/clients", recursive=True)
self._zk.delete("/publishers", recursive=True)
self._zk.delete("/election", recursive=True)
# create default paths
self.create_node("/services")
self.create_node("/clients")
self.create_node("/publishers")
self.create_node("/election")
self._debug = {
'subscription_expires': 0,
'oos_delete': 0,
'db_excepts': 0,
}
示例14: __init__
def __init__(self, module, server_list):
# logging
logger = logging.getLogger(module)
logger.setLevel(logging.INFO)
try:
handler = logging.handlers.RotatingFileHandler('/var/log/contrail/' + module + '-zk.log', maxBytes=10*1024*1024, backupCount=5)
except IOError:
print "Cannot open log file in /var/log/contrail/"
else:
log_format = logging.Formatter('%(asctime)s [%(name)s]: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
handler.setFormatter(log_format)
logger.addHandler(handler)
self._zk_client = \
kazoo.client.KazooClient(
server_list,
handler=kazoo.handlers.gevent.SequentialGeventHandler(),
logger=logger)
self._logger = logger
self._election = None
self._zk_sem = BoundedSemaphore(1)
self.connect()
示例15: ZookeeperClient
class ZookeeperClient(object):
def __init__(self, module, server_list):
# logging
logger = logging.getLogger(module)
logger.setLevel(logging.INFO)
try:
handler = logging.handlers.RotatingFileHandler('/var/log/contrail/' + module + '-zk.log', maxBytes=10*1024*1024, backupCount=5)
except IOError:
print "Cannot open log file in /var/log/contrail/"
else:
log_format = logging.Formatter('%(asctime)s [%(name)s]: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
handler.setFormatter(log_format)
logger.addHandler(handler)
self._zk_client = \
kazoo.client.KazooClient(
server_list,
handler=kazoo.handlers.gevent.SequentialGeventHandler(),
logger=logger)
self._logger = logger
self._election = None
self._zk_sem = BoundedSemaphore(1)
self.connect()
# end __init__
# reconnect
def reconnect(self):
self._zk_sem.acquire()
self.syslog("restart: acquired lock; state %s " % self._zk_client.state)
# initiate restart if our state is suspended or lost
if self._zk_client.state != "CONNECTED":
self.syslog("restart: starting ...")
try:
self._zk_client.stop()
self._zk_client.close()
self._zk_client.start()
self.syslog("restart: done")
except gevent.event.Timeout as e:
self.syslog("restart: timeout!")
except Exception as e:
self.syslog('restart: exception %s' % str(e))
except Exception as str:
self.syslog('restart: str exception %s' % str)
self._zk_sem.release()
# start
def connect(self):
while True:
try:
self._zk_client.start()
break
except gevent.event.Timeout as e:
self.syslog(
'Failed to connect with Zookeeper -will retry in a second')
gevent.sleep(1)
# Zookeeper is also throwing exception due to delay in master election
except Exception as e:
self.syslog('%s -will retry in a second' % (str(e)))
gevent.sleep(1)
self.syslog('Connected to ZooKeeper!')
# end
def syslog(self, msg):
if not self._logger:
return
self._logger.info(msg)
# end syslog
def _zk_listener(self, state):
if state == "CONNECTED":
self._election.cancel()
# end
def _zk_election_callback(self, func, *args, **kwargs):
self._zk_client.remove_listener(self._zk_listener)
func(*args, **kwargs)
# Exit if running master encounters error or exception
exit(1)
# end
def master_election(self, path, identifier, func, *args, **kwargs):
self._zk_client.add_listener(self._zk_listener)
while True:
self._election = self._zk_client.Election(path, identifier)
self._election.run(self._zk_election_callback, func, *args, **kwargs)
# end master_election
def create_node(self, path, value=None):
try:
if value is None:
value = uuid.uuid4()
self._zk_client.create(path, str(value), makepath=True)
except (kazoo.exceptions.SessionExpiredError,
kazoo.exceptions.ConnectionLoss):
self.reconnect()
return self.create_node(path, value)
except kazoo.exceptions.NodeExistsError:
current_value = self.read_node(path)
#.........这里部分代码省略.........