本文整理汇总了Python中gevent.lock.BoundedSemaphore类的典型用法代码示例。如果您正苦于以下问题:Python BoundedSemaphore类的具体用法?Python BoundedSemaphore怎么用?Python BoundedSemaphore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BoundedSemaphore类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, server_node, iface_cls, config):
self._section_name = utils.get_module(__name__)
self._logger = logging.getLogger(__name__)
self._host = server_node.split(":")[0]
self._port = int(server_node.split(":")[1])
self._iface_cls = iface_cls
self._get_conn_timeout = config.getint(self._section_name, "pool_timeout",
default=settings.DEFAULT_POOL_TIMEOUT)
self._socket_timeout = config.getint(self._section_name, "request_timeout",
default=settings.DEFAULT_REQUEST_TIMEOUT) * 1000
self._size = config.getint(self._section_name, "pool_size", default=settings.DEFAULT_POOL_SIZE)
self._c_module_serialize = config.getboolean(self._section_name, "c_module_serialize",
default=settings.USE_C_MODULE_SERIALIZE)
self._closed = False
if ASYNC_TAG:
from gevent.lock import BoundedSemaphore
from gevent import queue as Queue
self._semaphore = BoundedSemaphore(self._size)
self._connection_queue = Queue.LifoQueue(self._size)
self._QueueEmpty = Queue.Empty
else:
from threading import BoundedSemaphore
import Queue
self._semaphore = BoundedSemaphore(self._size)
self._connection_queue = Queue.LifoQueue(self._size)
self._QueueEmpty = Queue.Empty
示例2: XCATMessager
class XCATMessager(utils.Messager):
def __init__(self, sock):
self.sock = sock
self.sem = BoundedSemaphore(1)
def _send(self, d):
buf = json.dumps(d)
self.sem.acquire()
self.sock.sendall(utils.int2bytes(len(buf)) + buf.encode('utf-8'))
self.sem.release()
def info(self, msg):
d = {'type': MSG_TYPE, 'msg': {'type': 'info', 'data': msg}}
self._send(d)
def warn(self, msg):
d = {'type': MSG_TYPE, 'msg': {'type': 'warning', 'data': msg}}
self._send(d)
def error(self, msg, node=''):
d = {'type': MSG_TYPE, 'msg': {'type': 'error', 'node': node, 'data': msg}}
self._send(d)
def syslog(self, msg):
d = {'type': MSG_TYPE, 'msg': {'type': 'syslog', 'data': msg}}
self._send(d)
def info_with_host(self, msg):
d = {'type': MSG_TYPE, 'msg': {'type': 'info_with_host', 'data': msg}}
self._send(d)
def update_node_attributes(self, attribute, node, data):
d = {'type': DB_TYPE, 'attribute': {'name': attribute, 'method': 'set', 'type': 'node', 'node': node, 'value': data}}
self._send(d)
示例3: __init__
def __init__(self, serverIP, Plist):
self.serverIP = serverIP
self.baseUrl = "http://%s/" % serverIP
self.crypt = Account(Plist.userID, Plist.devideToken)
self.mlkLock = BoundedSemaphore(200)
self.IID = Plist.iid
self.VID = Plist.vid
示例4: __init__
def __init__(self, account_id, num_connections, readonly):
log.info("Creating Crispin connection pool", account_id=account_id, num_connections=num_connections)
self.account_id = account_id
self.readonly = readonly
self._queue = Queue(num_connections, items=num_connections * [None])
self._sem = BoundedSemaphore(num_connections)
self._set_account_info()
示例5: __init__
def __init__( self ):
self._canRead = Event()
self._canWrite = Event()
self._mutex = BoundedSemaphore( value = 1 )
self._readers = 0
self._isWriting = False
self._canRead.set()
self._canWrite.set()
示例6: _LeakySemaphore
class _LeakySemaphore(object):
def __init__(self, value = 1, maxSeconds = 10):
self._semaphore = BoundedSemaphore(value)
self._maxSeconds = maxSeconds
self._timer = None
self._leaked = 0
self._stopped = False
def _leak(self):
sleep(self._maxSeconds)
self._leaked += 1
self._semaphore.release()
@property
def inUse(self):
return self._semaphore._initial_value - self.semaphore.counter
@property
def waiting(self):
return len(self._semaphore._links)
def release(self):
if self._stopped:
return
if self._leaked > 0:
self._leaked -= 1
else:
self._semaphore.release()
def stop(self):
self._stopped = True
if self._timer is not None:
self._timer.kill(block = False)
self._timer = None
while self.waiting > 0:
self._semaphore.release()
sleep(0.1)
def acquire(self):
if self._stopped:
return
if self._semaphore.locked() and not self._timer:
self._timer = spawn(self._leak)
self._semaphore.acquire(blocking = True, timeout = None)
if self._timer is not None:
self._timer.kill(block = False)
self._timer = None
if self.waiting > 0:
self._timer = spawn(self._leak)
示例7: __init__
def __init__(self, account_id, num_connections, readonly):
log.info('Creating Crispin connection pool for account {} with {} '
'connections'.format(account_id, num_connections))
self.account_id = account_id
self.readonly = readonly
self._queue = Queue(num_connections, items=num_connections * [None])
self._sem = BoundedSemaphore(num_connections)
self._set_account_info()
示例8: __init__
def __init__(self, port, data_dir):
gevent.spawn(self.controller)
self.state = STATE_READY
self.workers = {}
self.jobs_tracker = {}
self.port = port
self.data_dir = data_dir
self.mapper_queue = Queue()
self.reducer_queue = Queue()
self.jobs_tracker_lock = BoundedSemaphore(1)
self.workers_lock = BoundedSemaphore(1)
示例9: __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 iter(range(size)):
self.lock.acquire()
for i in iter(range(size)):
gevent.spawn_later(self.SPAWN_FREQUENCY*i, self._addOne)
if self.keepalive:
gevent.spawn(self._keepalive_periodic)
示例10: test_concurrent_batching
def test_concurrent_batching(self):
lock = BoundedSemaphore(1)
lock.acquire() # now 0
N_CALLS = [0]
@batched()
def fn(arg_list):
N_CALLS[0] += 1
lock.acquire()
@batched()
def fn2(arg_list):
N_CALLS[0] += 1
lock.release()
@batch_context
def test():
a, b = spawn(fn), spawn(fn2)
self.assertEquals(0, N_CALLS[0])
a.get(), b.get()
test() # shouldn't hang.
示例11: acquire
def acquire(self, blocking=True, timeout=-1):
# Transform the default -1 argument into the None that our
# semaphore implementation expects, and raise the same error
# the stdlib implementation does.
if timeout == -1:
timeout = None
if not blocking and timeout is not None:
raise ValueError("can't specify a timeout for a non-blocking call")
if timeout is not None:
if timeout < 0:
# in C: if(timeout < 0 && timeout != -1)
raise ValueError("timeout value must be strictly positive")
if timeout > self._TIMEOUT_MAX:
raise OverflowError('timeout value is too large')
return BoundedSemaphore.acquire(self, blocking, timeout)
示例12: __init__
def __init__(self, api, token_address):
assert isinstance(api, RaidenAPI)
self.ready = Event()
self.api = api
self.token_address = token_address
existing_channels = self.api.get_channel_list(
api.raiden.default_registry.address,
self.token_address,
)
open_channels = [
channel_state
for channel_state in existing_channels
if channel.get_status(channel_state) == CHANNEL_STATE_OPENED
]
if len(open_channels) == 0:
token = self.api.raiden.chain.token(self.token_address)
if not token.balance_of(self.api.raiden.address) > 0:
raise ValueError('not enough funds for echo node %s for token %s' % (
pex(self.api.raiden.address),
pex(self.token_address),
))
self.api.token_network_connect(
self.api.raiden.default_registry.address,
self.token_address,
token.balance_of(self.api.raiden.address),
initial_channel_target=10,
joinable_funds_target=.5,
)
self.last_poll_offset = 0
self.received_transfers = Queue()
self.stop_signal = None # used to signal REMOVE_CALLBACK and stop echo_workers
self.greenlets = list()
self.lock = BoundedSemaphore()
self.seen_transfers = deque(list(), TRANSFER_MEMORY)
self.num_handled_transfers = 0
self.lottery_pool = Queue()
# register ourselves with the raiden alarm task
self.api.raiden.alarm.register_callback(self.echo_node_alarm_callback)
self.echo_worker_greenlet = gevent.spawn(self.echo_worker)
log.info('Echo node started')
示例13: __init__
def __init__(self, value = 1, maxSeconds = 10):
self._semaphore = BoundedSemaphore(value)
self._maxSeconds = maxSeconds
self._timer = None
self._leaked = 0
self._stopped = False
示例14: RWLock
class RWLock( object ):
def __init__( self ):
self._canRead = Event()
self._canWrite = Event()
self._mutex = BoundedSemaphore( value = 1 )
self._readers = 0
self._isWriting = False
self._canRead.set()
self._canWrite.set()
def rLock( self ):
isReady = False
while not isReady:
self._canRead.wait()
self._mutex.acquire( blocking = True, timeout = None )
if not self._isWriting:
self._canWrite.clear()
self._readers += 1
isReady = True
self._mutex.release()
def rUnlock( self ):
self._mutex.acquire( blocking = True, timeout = None )
self._readers -= 1
if 0 == self._readers:
self._canWrite.set()
self._mutex.release()
def wLock( self ):
isReady = False
while not isReady:
self._canRead.clear()
self._canWrite.wait()
self._mutex.acquire( blocking = True, timeout = None )
if not self._isWriting and 0 == self._readers:
isReady = True
self._isWriting = True
self._canWrite.clear()
self._mutex.release()
def wUnlock( self ):
self._mutex.acquire( blocking = True, timeout = None )
self._isWriting = False
self._canWrite.set()
self._canRead.set()
self._mutex.release()
def writer( self ):
return _rwlock_w( self )
def reader( self ):
return _rwlock_r( self )
示例15: LibMLK
class LibMLK(object):
def __init__(self, serverIP, Plist):
self.serverIP = serverIP
self.baseUrl = "http://%s/" % serverIP
self.crypt = Account(Plist.userID, Plist.devideToken)
self.mlkLock = BoundedSemaphore(200)
self.IID = Plist.iid
self.VID = Plist.vid
def __init__(self):
pass
@property
def headers(self):
return {
"VID": self.VID,
"PID": "-",
"IID": self.IID,
"DEVICE_INFO": "iPad2,1:::iPhone OS 8.1.2",
"Device": "ios",
"AppVersion": 28,
"APP_ID_3": self.crypt.deviceToken,
"APP_ID_2": self.crypt.hashedUserID,
"APP_ID_1": self.crypt.cryptedUserID,
"Encrypted": True,
"User-Agent": "toto/1.1.25.2 CFNetwork/711.1.16 Darwin/14.0.0",
"Accept-Language": "zh-cn",
"Accept": "application/json"
}
def _post(self, url, params={}, data={}):
data["_method"] = "GET"
data = urllib.urlencode(data)
data = self.crypt.encrypt(data)
url = urlparse.urljoin(self.baseUrl, url)
if len(params) > 0:
e = self.crypt.encrypt(urllib.urlencode(params)).encode("base64").replace("\n", "")
url = "%s?e=%s" % (url, e)
ret = None
try:
self.mlkLock.acquire()
ret = requests.post(url, data=data, headers=self.headers, proxies=proxies)
except:
traceback.print_exc()
finally:
self.mlkLock.release()
if ret is None:
raise BaseException()
if "encrypted" in ret.headers and ret.headers["encrypted"] == "true":
rtn = self.crypt.decrypt(ret.content)
else:
rtn = ret.content
return rtn
def get(self, url, params={}, data={}):
url = urlparse.urlparse(url)
path = url.path
query = dict(urlparse.parse_qsl(url.query))
query.update(params)
return self._post(path, params=query, data=data)
def setUsername(self, name):
ret = self._post("users/update", data={"user_name": name})
self.user_name = name
return json.loads(ret)
def finishTutorial(self):
ret = self._post("users/update",
data={"user_name": self.user_name, "tutorial_finish": True})
return json.loads(ret)
def getMessages(self, page_type="Home"):
params = {
"last_read_at": int(time.time()),
"page_type": page_type
}
ret = self._post("users/messages", params=params)
return json.loads(ret)
def getStages(self):
ret = self._post("stages")
return json.loads(ret)
def getAreas(self, stage_id):
ret = self._post("areas", params={"stage_id": stage_id})
return json.loads(ret)
def getMonsters(self):
ret = self._post("user_monsters")
return json.loads(ret)
def getDecks(self):
ret = self._post("user_decks")
return json.loads(ret)
def getUnits(self):
ret = self._post("user_units")
return json.loads(ret)
#.........这里部分代码省略.........