Python gevent.lock() Examples
The following are 30
code examples of gevent.lock().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
gevent
, or try the search function
.
Example #1
Source File: gipc.py From gipc with MIT License | 6 votes |
def __init__(self): global _all_handles # Generate label of text/unicode type from three random bytes. self._id = codecs.encode(os.urandom(3), "hex_codec").decode("ascii") self._legit_pid = os.getpid() self._make_nonblocking() # Define lock for synchronizing access to this handle within the current # process. Note that a `gevent.lock.Semaphore` instance lives on the # heap of the current process and cannot be used to synchronize access # across multiple processes. That is, this lock is only meaningful in # the current process. This is especially important to consider when the # platform supports fork()ing. self._lock = gevent.lock.Semaphore(value=1) self._closed = False _all_handles.append(self)
Example #2
Source File: eth_service.py From pyethapp with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, app): self.config = app.config self.db = app.services.db assert self.db is not None super(ChainService, self).__init__(app) log.info('initializing chain') coinbase = app.services.accounts.coinbase self.chain = Chain(self.db, new_head_cb=self._on_new_head, coinbase=coinbase) log.info('chain at', number=self.chain.head.number) self.synchronizer = Synchronizer(self, force_sync=None) self.block_queue = Queue(maxsize=self.block_queue_size) self.transaction_queue = Queue(maxsize=self.transaction_queue_size) self.add_blocks_lock = False self.add_transaction_lock = gevent.lock.Semaphore() self.broadcast_filter = DuplicatesFilter() self.on_new_head_cbs = [] self.on_new_head_candidate_cbs = []
Example #3
Source File: queryprocessor.py From minemeld-core with Apache License 2.0 | 6 votes |
def __init__(self, comm, store, config=None): if config is None: config = {} self._stop = gevent.event.Event() self.max_concurrency = config.get('max_concurrency', 10) self.redis_config = config.get('redis', {}) self.store = store self.queries_lock = gevent.lock.BoundedSemaphore() self.queries = {} comm.request_rpc_server_channel( QUERY_QUEUE, self, allowed_methods=['query', 'kill_query'] )
Example #4
Source File: ws.py From janus-cloud with GNU Affero General Public License v3.0 | 6 votes |
def __init__(self, url, recv_msg_cbk=None, close_cbk=None, protocols=None, msg_encoder=None, msg_decoder=None): # patch socket.sendall to protect it with lock, # in order to prevent sending data from multiple greenlets concurrently WebSocketClient.__init__(self, url, protocols=protocols) self._msg_encoder = msg_encoder or self.DEFAULT_ENCODER self._msg_decoder = msg_decoder or self.DEFAULT_DECODER lock = RLock() _sendall = self.sock.sendall self._recv_msg_cbk = recv_msg_cbk self._close_cbk = close_cbk def sendall(data): lock.acquire() try: _sendall(data) except Exception: raise finally: lock.release() self.sock.sendall = sendall self.connect() log.info('Created {0}'.format(self))
Example #5
Source File: __init__.py From rowboat with MIT License | 6 votes |
def run(self, job): lock = None if self.task.global_lock: lock = rdb.lock('{}:{}'.format( self.task.name, self.task.global_lock( *job['args'], **job['kwargs'] ) )) lock.acquire() if self.task.max_concurrent: self.lock.acquire() self.process(job) if lock: lock.release() if self.task.max_concurrent: self.lock.release()
Example #6
Source File: timing.py From rowboat with MIT License | 5 votes |
def _execute(self): """ Executes the Eventual function, guarded by a lock. """ with self._mutex: if self._waiter_greenlet: self._waiter_greenlet.kill() self._waiter_greenlet = None self.function() self._next_execution_time = None
Example #7
Source File: __init__.py From rowboat with MIT License | 5 votes |
def __init__(self, name, task): self.name = name self.task = task self.lock = Semaphore(task.max_concurrent)
Example #8
Source File: base.py From TcpRoute with GNU General Public License v2.0 | 5 votes |
def __init__(self, config): self.type = config.get('type', None) self.config = config import upstream as upstream_mod upconfig = config.get('upstream', None) if upconfig: uptype = upconfig.get("type", None) if uptype is None: raise ConfigError(u'[配置错误] upstream 未配置 type !') Upstream = upstream_mod.get_upstream(uptype) if Upstream is None: raise ConfigError(u'[配置错误] upstream type %s 不被支持!' % uptype) self.upstream = Upstream(upconfig) pass else: if self.type != 'direct': self.upstream = upstream_mod.get_upstream('direct')({'type':'direct'}) else: self.upstream = _socket self.http_pool = HttpPool(self,lock=BoundedSemaphore)
Example #9
Source File: internal.py From rowboat with MIT License | 5 votes |
def load(self, ctx): super(InternalPlugin, self).load(ctx) self.events = RedisSet(rdb, 'internal:tracked-events') self.session_id = None self.lock = Semaphore() self.cache = []
Example #10
Source File: internal.py From rowboat with MIT License | 5 votes |
def on_gateway_event(self, event): if event['t'] not in self.events: return with self.lock: self.cache.append(event)
Example #11
Source File: gipc.py From gipc with MIT License | 5 votes |
def __getstate__(self): # Hook into the pickling process. The default state which is pickled is # self.__dict__. Remove the lock from it. The general motivation is: # conceptually, the lock's state becomes meaningless in the moment the # handle is pickled around. The specific motivation is: # `gevent.lock.Semaphore` is not picklable with some versions of gevent # on Windows, see https://github.com/jgehrcke/gipc/issues/36. Hence, do # not even try to pickle it to the child process. state = self.__dict__.copy() del state['_lock'] return state
Example #12
Source File: ws.py From janus-cloud with GNU Affero General Public License v3.0 | 5 votes |
def opened(self): # patch socket.sendall to protect it with lock, # in order to prevent sending data from multiple greenlets concurrently lock = RLock() _sendall = self.sock.sendall def sendall(data): lock.acquire() try: _sendall(data) except Exception: raise finally: lock.release() self.sock.sendall = sendall # start check idle if self._pingpong_trigger: self._last_active_ts = get_monotonic_time() self._pingpong_check_greenlet = gevent.spawn(self._pingpong_check_routine) # create app try: if not self.environ.get('QUERY_STRING'): query = {} else: query = urllib.parse.parse_qs(self.environ['QUERY_STRING'], keep_blank_values=True) for key, value in query.items(): query[key] = value[0] self._recv_msg_cbk = self.environ['app.recv_msg_cbk'] self._closed_cbk = self.environ['app.closed_cbk'] log.info('Created {0}'.format(self)) except Exception: log.exception('Failed to create app for {0}'.format(self)) raise
Example #13
Source File: gevent.py From profiling with BSD 3-Clause "New" or "Revised" License | 5 votes |
def profile_periodically(self): with self.lock: for __ in self.profiling(): gevent.sleep(self.interval)
Example #14
Source File: gevent.py From profiling with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, listener, profiler=None, interval=INTERVAL, log=LOG, pickle_protocol=PICKLE_PROTOCOL, **server_kwargs): StreamServer.__init__(self, listener, **server_kwargs) ProfilingServer.__init__(self, profiler, interval, log, pickle_protocol) self.lock = Semaphore() self.profiling_greenlet = None
Example #15
Source File: wechat_qy.py From satori with Apache License 2.0 | 5 votes |
def __init__(self, conf): super(WechatQYBackend, self).__init__(conf) self.lock = RLock() self.token_cache = {}
Example #16
Source File: main.py From satori with Apache License 2.0 | 5 votes |
def values(self): with self.lock: return list(self.alarms.values())
Example #17
Source File: main.py From satori with Apache License 2.0 | 5 votes |
def dumps(self): with self.lock: return json.dumps(self.alarms)
Example #18
Source File: main.py From satori with Apache License 2.0 | 5 votes |
def tick(self): with self.lock: for k, v in list(self.alarms.items()): if v['status'] in self.ticking_states: self.transit(id=k, action='TICK')
Example #19
Source File: main.py From satori with Apache License 2.0 | 5 votes |
def transit(self, id=None, action=None, ev=None): assert bool(id and action) ^ bool(ev) with self.lock: if ev: id = ev['id'] event = ev['status'] last = self.alarms.get(id) when = last['status'] if last else 'OK' action = ev['status'] else: ev = self.alarms.get(id) if not ev: log.info('AlarmDFA: Id %s does not exist', id) return 'ERROR' when = ev['status'] for find in [(when, action), ('@ALWAYS', action)]: for cond, (handler, valid) in self.trans: if find == cond: log.debug('Handling %s by %s', find, handler.__name__) new = handler(self, when, action, ev) assert new in valid or new == 'ERROR', 'Invalid transition: %s handled by %s, want to go %s' % (find, handler.__name__, new) ev['status'] = new if new == 'OK': self.alarms.pop(id, '') else: self.alarms[id] = ev return new else: log.error("Can't handle %s in state %s", event, when) return 'ERROR'
Example #20
Source File: main.py From satori with Apache License 2.0 | 5 votes |
def __init__(self): self.alarms = {} self.lock = RLock()
Example #21
Source File: wechat_qy.py From satori with Apache License 2.0 | 5 votes |
def __init__(self, conf): super(WechatQYBackend, self).__init__(conf) self.lock = RLock() self.token_cache = {}
Example #22
Source File: sockets.py From Dallinger with MIT License | 5 votes |
def __init__(self, ws, lag_tolerance_secs=0.1): self.ws = ws self.lag_tolerance_secs = lag_tolerance_secs # This lock is used to make sure that multiple greenlets # cannot send to the same socket concurrently. self.send_lock = Semaphore()
Example #23
Source File: eth_service.py From pyethapp with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_block(self, t_block, proto): "adds a block to the block_queue and spawns _add_block if not running" self.block_queue.put((t_block, proto)) # blocks if full if not self.add_blocks_lock: self.add_blocks_lock = True # need to lock here (ctx switch is later) gevent.spawn(self._add_blocks)
Example #24
Source File: utils.py From minemeld-core with Apache License 2.0 | 5 votes |
def lock(self): self.m2.acquire() self.num_writers += 1 if self.num_writers == 1: self.r.acquire() self.m2.release() self.w.acquire()
Example #25
Source File: utils.py From minemeld-core with Apache License 2.0 | 5 votes |
def __init__(self): self.num_readers = 0 self.num_writers = 0 self.m1 = gevent.lock.Semaphore(1) self.m2 = gevent.lock.Semaphore(1) self.m3 = gevent.lock.Semaphore(1) self.w = gevent.lock.Semaphore(1) self.r = gevent.lock.Semaphore(1)
Example #26
Source File: aaa.py From minemeld-core with Apache License 2.0 | 5 votes |
def init_app(app): global PREVENT_WRITE global PREVENT_WRITE_GUARD app.config['REMEMBER_COOKIE_NAME'] = None # to block remember cookie LOGIN_MANAGER.init_app(app) # initialize PREVENT_WRITE PREVENT_WRITE_GUARD = gevent.lock.BoundedSemaphore() PREVENT_WRITE = None
Example #27
Source File: stream.py From gnsq with BSD 3-Clause "New" or "Revised" License | 5 votes |
def send(self, data): self.ensure_connection() with self.lock: try: return self.socket.sendall(data) except socket.error as error: six.raise_from(NSQSocketError(*error.args), error)
Example #28
Source File: stream.py From gnsq with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, address, port, timeout, buffer_size=PAGESIZE, lock_class=Semaphore): self.address = address self.port = port self.timeout = timeout self.buffer = b'' self.buffer_size = buffer_size self.socket = None self.lock = lock_class()
Example #29
Source File: gipc.py From gipc with MIT License | 5 votes |
def __setstate__(self, state): # Restore instance attributes. self.__dict__.update(state) # Create a fresh lock for synchronizing the access to this handle in the # current process. self._lock = gevent.lock.Semaphore(value=1)
Example #30
Source File: wechat_qy.py From satori with Apache License 2.0 | 4 votes |
def get_access_token(self, corp_id, secret): key = '%s:%s' % (corp_id, secret) def get(): token, expire = self.token_cache.get(key, (None, 0)) if time.time() - expire >= 7100: token = None return token token = get() if token: return token with self.lock: token = get() if token: return token for _ in range(3): resp = requests.get('https://qyapi.weixin.qq.com/cgi-bin/gettoken', params={'corpid': corp_id, 'corpsecret': secret}, timeout=10, ) if not resp.ok: raise Exception('Error getting access token: %s' % resp.content) ret = resp.json() code = ret.get('errcode', 0) if code == 42001: gevent.sleep(2) continue elif code: raise Exception('Error getting access token: %s' % ret['errmsg']) token = ret['access_token'] break else: self.logger.error('Get access token max retry exceeded') return None self.token_cache[key] = (token, time.time()) return token