我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用asyncio.Lock()。
def __init__(self, instance): """ Initiate, should only be done from the core instance. :param instance: Instance. :type instance: pyplanet.core.instance.Instance """ self._instance = instance self.lock = asyncio.Lock() # The matchsettings contains the name of the current loaded matchsettings file. self._matchsettings = None # The maps contain a list of map instances in the order that are in the current loaded list. self._maps = set() # The current map will always be in this variable. The next map will always be here. It will be updated. once # it's updated it should be send to the dedicated to queue the next map. self._previous_map = None self._current_map = None self._next_map = None
def __init__(self, instance): """ Initiate, should only be done from the core instance. :param instance: Instance. :type instance: pyplanet.core.instance.Instance """ self._instance = instance self._performance_mode = False # self.lock = asyncio.Lock() # Online contains all currently online players. self._online = set() self._online_logins = set() # Counters. self._counter_lock = asyncio.Lock() self._total_count = 0 self._players_count = 0 self._spectators_count = 0
def get_player(self, login=None, pk=None, lock=True): """ Get player by login or primary key. :param login: Login. :param pk: Primary Key identifier. :param lock: Lock for a sec when receiving. :return: Player or exception if not found :rtype: pyplanet.apps.core.maniaplanet.models.Player """ try: if login: return await Player.get_by_login(login) elif pk: return await Player.get(pk=pk) else: raise PlayerNotFound('Player not found.') except DoesNotExist: if lock: await asyncio.sleep(4) return await self.get_player(login=login, pk=pk, lock=False) else: raise PlayerNotFound('Player not found.')
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.lock = asyncio.Lock() self.current_records = [] self.widget = None self.setting_chat_announce = Setting( 'chat_announce', 'Minimum index for chat announce', Setting.CAT_BEHAVIOUR, type=int, description='Minimum record index needed for public new record/recordchange announcement (0 for disable).', default=50 ) self.setting_record_limit = Setting( 'record_limit', 'Local Records limit', Setting.CAT_BEHAVIOUR, type=int, description='Limit for the amount of Local Records displayed in-game (0 for disable).', default=100 )
def add_item(self, success_cb_id, firstname, lastname, date, source, destination, train_num, ct_letter=None): scan_id = uuid4().hex self.__state[scan_id] = dict( success_cb_id=success_cb_id, firstname=firstname, lastname=lastname, date=date, source=source, destination=destination, train_num=train_num, ct_letter=ct_letter, lock=asyncio.Lock(), attempts=0, error=None) return scan_id
def __init__(self, root, cfg): """ :param dict cfg: provider config """ self.root = root self.cfg = cfg self.name = cfg["name"] self.pubkey = os.path.expanduser( root.config.data["ssh-key"]["default"]["public"]) self.privkey = root.config.data["ssh-key"]["default"]["private"] self.gethost_lock = asyncio.Lock(loop=root.loop) self.job_host = {} self.hosts = [] for host_cfg in cfg.get("hosts"): self.hosts.append(Host(self, SSH(loop=self.root.loop, **host_cfg)))
def __init__(self, ssh_conf, provider, root): """ :param dict ssh_conf: item from hosts from provider :param Host host: """ self.provider = provider self.root = root self.config = provider.config self.image_locks = {} self._job_vms = {} self._job_bridge_numbers = {} ssh_conf.setdefault("username", "root") ssh_conf["keys"] = root.config.get_ssh_keys(keytype="private") self.ssh = SSH(root.loop, **ssh_conf) self.la = 0.0 self.free = 0 storage_cf = self.config["storage"] self.storage = BACKENDS[storage_cf["backend"]](self.ssh, **storage_cf) self.bridge_lock = asyncio.Lock(loop=root.loop)
def test_context_manager_cant_reuse(self): lock = asyncio.Lock(loop=self.loop) @asyncio.coroutine def acquire_lock(): return (yield from lock) # This spells "yield from lock" outside a generator. cm = self.loop.run_until_complete(acquire_lock()) with cm: self.assertTrue(lock.locked()) self.assertFalse(lock.locked()) with self.assertRaises(AttributeError): with cm: pass
def __init__(self, address, *, password=None, parser=None, encoding=None, minsize, maxsize, connection_cls=None, timeout=None, loop=None): assert isinstance(minsize, int) and minsize >= 0, ("minsize must be int >=0", minsize, type(minsize)) assert isinstance(maxsize, int) and maxsize >= minsize, ( "maxsize must be int >= minsize", maxsize, type(maxsize), minsize) if loop is None: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) self._address = address self._password = password self._parser_class = parser self._timeout = timeout self._loop = loop self._used = set() self._connection_cls = connection_cls self._pool = collections.deque(maxlen=maxsize) self._minsize = minsize self._maxsize = maxsize self._encoding = encoding # ??release????????????????????????????? self._cond = asyncio.Condition(lock=asyncio.Lock(loop=loop), loop=loop) self._waiter = None self._closing = False self._closed = False
def __init__(self, host: str = 'localhost', port: int = 5672, login: str = 'guest', password: str = 'guest', virtual_host: str = '/', ssl: bool = False, *, loop=None, **kwargs): self.loop = loop if loop else asyncio.get_event_loop() self.future_store = FutureStore(loop=self.loop) self.__credentials = PlainCredentials(login, password) if login else None self.__connection_parameters = ConnectionParameters( host=host, port=port, credentials=self.__credentials, virtual_host=virtual_host, ssl=ssl, **kwargs ) self._channels = dict() self._connection = None self.__closing = None self.__write_lock = asyncio.Lock(loop=self.loop)
def locked(obj): """Return object specfic volatile asyncio lock. This is used together with "with" syntax to asynchronously lock objects while they are mutated to prevent other concurrent requests accessing the object by accident. :param obj: object to be locked Example:: with locked(ob): # do something """ try: return ASYNCIO_LOCKS.get(obj._p_oid) or \ ASYNCIO_LOCKS.setdefault(obj._p_oid, asyncio.Lock()) except AttributeError: # obj has no _p_oid return ASYNCIO_LOCKS.get(id(obj)) or \ ASYNCIO_LOCKS.setdefault(id(obj), asyncio.Lock())
def __init__(self, dsn=None, partition=None, read_only=False, name=None, pool_size=13, transaction_strategy='resolve', conn_acquire_timeout=20, cache_strategy='dummy', **options): super(PostgresqlStorage, self).__init__( read_only, transaction_strategy=transaction_strategy, cache_strategy=cache_strategy) self._dsn = dsn self._pool_size = pool_size self._partition_class = partition self._read_only = read_only self.__name__ = name self._read_conn = None self._lock = asyncio.Lock() self._conn_acquire_timeout = conn_acquire_timeout self._options = options self._connection_options = {} self._connection_initialized_on = time.time()
def _initialize(bot): bot.spawn_lock = asyncio.Lock() config = bot.config.get_option("spawn") if not config: return cmds = config.get("commands") # override the load logic and register our commands directly get_location = False for cmd, cnf in cmds.items(): command.register(_spawn, admin=True, final=True, name=cmd) if cnf.get("allow_location"): get_location = True logger.info("spawn - %s", ", ".join(['*' + cmd for cmd in cmds])) plugins.register_admin_command(list(cmds)) if get_location: global _MAP_MATCH _MAP_MATCH = re.compile(config.get("map_regex", _MAP_REGEX), re.IGNORECASE|re.MULTILINE) plugins.register_handler(_location_handler, type="message")
def init(self): """ config: connection: host: port: auth: username: password: virtual_host: exchange: name: type: queue: route_key: wait: format: [json|msg] """ await super().init() self._started = False self._lock = asyncio.Lock(loop=self.loop) await self._lock.acquire() self.context.on_start.append(self.start) self.context.on_stop.append(self.stop)
def __init__(self, url='localhost:9091', loop=None): # Use double underscores because TransmissionAPI inherits from this # class; this way, we don't have to worry about name collisions. self.loop = loop if loop is not None else asyncio.get_event_loop() self.__url = TransmissionURL(url) self.__headers = {'content-type': 'application/json'} self.__session = None self.__connect_exception = None self.__connection_lock = asyncio.Lock(loop=loop) self.__request_lock = asyncio.Lock(loop=loop) self.__connection_tested = False self.__timeout = TIMEOUT self.__version = None self.__rpcversion = None self.__rpcversionmin = None self.__on_connecting = Signal() self.__on_connected = Signal() self.__on_disconnected = Signal() self.__on_error = Signal()
def __init__(self, bot): config = bot.config['ddmbot'] self._config_ds_token_timeout = datetime.timedelta(seconds=int(config['ds_token_timeout'])) self._config_ds_notify_time = datetime.timedelta(seconds=int(config['ds_notify_time'])) self._config_ds_remove_time = datetime.timedelta(seconds=int(config['ds_remove_time'])) self._config_dj_notify_time = datetime.timedelta(seconds=int(config['dj_notify_time'])) self._config_dj_remove_time = datetime.timedelta(seconds=int(config['dj_remove_time'])) self._bot = bot self._lock = asyncio.Lock(loop=bot.loop) self._tokens = dict() # maps token (string) -> (timestamp, user) self._listeners = dict() # maps discord_id (int) -> ListenerInfo self._queue = collections.deque() # # API for displaying information #
def send_raw(self, method, params=None, expectedTypes=None): self._i += 1 i = self._i self._recv_data_lock[i] = asyncio.Lock() await self._recv_data_lock[i].acquire() await self._soc.send(json.dumps({'id': i, 'method': method, 'params': params})) await self._recv_data_lock[i].acquire() del self._recv_data_lock[i] resp = self._method_responses.pop(i) if 'result' in resp: result = resp['result'] if expectedTypes is not None: return self._unpack_response(method, result) else: return result elif 'error' in resp: raise FailResponse(resp['error']['message'], resp['error']['code']) else: raise RuntimeError('Unknown data came: {0}'.format(resp))
def __init__(self, bot): super().__init__(bot) self.bot.simple_exc.extend([StarError, StarAddError, StarRemoveError]) # prevent race conditions self._locks = collections.defaultdict(asyncio.Lock) # janitor #: the janitor semaphore keeps things up and running # by only allowing 1 janitor task each time. # a janitor task cleans stuff out of mongo self.janitor_semaphore = asyncio.Semaphore(1) # collectiones self.starboard_coll = self.config.jose_db['starboard'] self.starconfig_coll = self.config.jose_db['starconfig']
def __init__(self, name, **options): self.name = name self.object_hook = options.pop('object_hook', None) self.encoder = options.pop('encoder', None) try: hook = options.pop('hook') except KeyError: pass else: self.object_hook = hook.from_json self.encoder = _create_encoder(hook) self.loop = options.pop('loop', asyncio.get_event_loop()) self.lock = asyncio.Lock() if options.pop('load_later', False): self.loop.create_task(self.load()) else: self.load_from_file()
def __init__(self, stream_id, window_getter, loop=None): if loop is None: loop = asyncio.get_event_loop() self._stream_id = stream_id self._window_getter = window_getter self._wlock = asyncio.Lock(loop=loop) self._window_open = CallableEvent(self._is_window_open, loop=loop) self._rlock = asyncio.Lock(loop=loop) self._buffers = deque() self._buffer_size = 0 self._buffer_ready = asyncio.Event(loop=loop) self._response = asyncio.Future(loop=loop) self._trailers = asyncio.Future(loop=loop) self._eof_received = False self._closed = False
def __init__(self, connection): """ Constructs this handler on a given validator connection. Args: connection (messaging.Connection): the validator connection """ self._connection = connection self._latest_state_delta_event = None self._subscribers = [] self._subscriber_lock = asyncio.Lock() self._delta_task = None self._listening = False self._accepting = True self._connection.on_connection_state_change( ConnectionEvent.DISCONNECTED, self._handle_disconnect) self._connection.on_connection_state_change( ConnectionEvent.RECONNECTED, self._handle_reconnection)
def __init__(self, *, config: Config, loop: asyncio.BaseEventLoop=_init_loop(), show_progress: bool=True): self.config = config self.show_progress = show_progress db_engine = create_engine(f'sqlite:///{config.config_dir/config.db_name}') ModelBase.metadata.create_all(db_engine) self.db = sessionmaker(bind=db_engine)() self.client = ClientSession(connector=TCPConnector(limit_per_host=10, loop=loop), headers={'User-Agent': _UA_STRING}, loop=loop) self.resolvers = {n: r(manager=self) for n, r in BaseResolver.__members__.items()} self.runner = _Runner(self) self._loop = loop self._resolver_lock = asyncio.Lock(loop=loop) self._tpes = [ThreadPoolExecutor(max_workers=1), ThreadPoolExecutor(max_workers=1)]
def __init__(self, hostname, username=None, known_hosts=None, keys=None, port=22, gateway=None, # if another SshProxy is given, it is used as an ssh gateway formatter=None, verbose=None, debug=False, timeout=30): self.hostname = hostname self.username = username self.known_hosts = known_hosts self.keys = keys if keys is not None else [] self.port = int(port) self.gateway = gateway # if not specified we use a basic colon formatter self.formatter = formatter or ColonFormatter("") if verbose is not None: self.formatter.verbose = verbose self.debug = debug self.timeout = timeout # self.conn, self.sftp_client = None, None # critical sections require mutual exclusions self._connect_lock = asyncio.Lock() self._disconnect_lock = asyncio.Lock() # make this an asynchroneous context manager # async with SshProxy(...) as ssh: #
def __init__(self, hass: HomeAssistantType, consider_home: timedelta, track_new: bool, devices: Sequence) -> None: """Initialize a device tracker.""" self.hass = hass self.devices = {dev.dev_id: dev for dev in devices} self.mac_to_dev = {dev.mac: dev for dev in devices if dev.mac} self.consider_home = consider_home self.track_new = track_new self.group = None # type: group.Group self._is_updating = asyncio.Lock(loop=hass.loop) for dev in devices: if self.devices[dev.dev_id] is not dev: _LOGGER.warning('Duplicate device IDs detected %s', dev.dev_id) if dev.mac and self.mac_to_dev[dev.mac] is not dev: _LOGGER.warning('Duplicate device MAC addresses detected %s', dev.mac)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.current_bills = dict() self.min_donation = 10 self.public_appreciation = 100 self.lock = asyncio.Lock()
def __init__(self, connection): self.connection = weakref.ref(connection) self.reconnecting = asyncio.Lock(loop=connection.loop) self.close_called = asyncio.Event(loop=connection.loop)
def __init__(self, session=None, request_timeout=10): self._session = session self.request_timeout = request_timeout self._token_lock = asyncio.Lock() self._token = None self._token_date = 0 self._token_max_age = 600 # 10 minutes self._user_agent = None
def __init__(self, provider, ssh): self.provider = provider self.ssh = ssh self.job_vm = {} self._building_images = collections.defaultdict( functools.partial(asyncio.Lock, loop=provider.root.loop)) self._create_opts = ["-B", "btrfs"]
def build_image(self, name): LOG.info("Building image %s" % name) self.image_locks.setdefault(name, asyncio.Lock(loop=self.root.loop)) with (yield from self.image_locks[name]): if (yield from self.storage.exist(name)): LOG.debug("Image %s exist" % name) return image_conf = self.config["images"][name] parent = image_conf.get("parent") if parent: yield from self.build_image(parent) yield from self.storage.clone(parent, name) else: url = image_conf.get("url") if url: yield from self.storage.download(name, url) yield from self.storage.snapshot(name) return # TODO: support build_script for downloaded images build_scripts = image_conf.get("build-scripts") if build_scripts: vm = yield from self.boot_image(name) try: for script in build_scripts: script = self.root.config.data["script"][script] LOG.debug("Running build script %s" % script) yield from self._run_script(vm, script) yield from vm.shutdown(storage=False) except: LOG.exception("Error building image") yield from vm.destroy() raise else: LOG.debug("No build script for image %s" % name) yield from asyncio.sleep(4) yield from self.storage.snapshot(name)
def __init__(self, root, config): """ :param config: full provider config """ self.root = root self.config = config self.name = config["name"] self.key = root.config.get_ssh_key() self.last = time.time() self._job_host_map = {} self._get_host_lock = asyncio.Lock(loop=root.loop)
def __init__(self, loop, hostname=None, username=None, keys=None, port=22, cb=None, jumphost=None, password=None): """ :param SSH jumphost: """ self.loop = loop self.username = username or pwd.getpwuid(os.getuid()).pw_name self.hostname = hostname self.password = password self.port = port self.cb = cb self.jumphost = jumphost self._forwarded_remote_ports = [] if keys: self.keys = [] for key in keys: if key.startswith("~"): key = os.path.expanduser(key) self.keys.append(key) else: self.keys = None self._connecting = asyncio.Lock(loop=loop) self._connected = asyncio.Event(loop=loop) self.closed = asyncio.Event(loop=loop) self.closed.set()
def main(loop): # Create and acquire a shared lock. lock = asyncio.Lock() print('acquiring the lock before starting coroutines') await lock.acquire() print('lock acquired: {}'.format(lock.locked())) # Schedule a callback to unlock the lock. loop.call_later(0.1, functools.partial(unlock, lock)) # Run the coroutines that want to use the lock. print('waiting for coroutines') await asyncio.wait([coro1(lock), coro2(lock)]),
def test_ctor_loop(self): loop = mock.Mock() lock = asyncio.Lock(loop=loop) self.assertIs(lock._loop, loop) lock = asyncio.Lock(loop=self.loop) self.assertIs(lock._loop, self.loop)
def test_ctor_noloop(self): asyncio.set_event_loop(self.loop) lock = asyncio.Lock() self.assertIs(lock._loop, self.loop)
def test_repr(self): lock = asyncio.Lock(loop=self.loop) self.assertTrue(repr(lock).endswith('[unlocked]>')) self.assertTrue(RGX_REPR.match(repr(lock))) @asyncio.coroutine def acquire_lock(): yield from lock self.loop.run_until_complete(acquire_lock()) self.assertTrue(repr(lock).endswith('[locked]>')) self.assertTrue(RGX_REPR.match(repr(lock)))
def test_lock(self): lock = asyncio.Lock(loop=self.loop) @asyncio.coroutine def acquire_lock(): return (yield from lock) res = self.loop.run_until_complete(acquire_lock()) self.assertTrue(res) self.assertTrue(lock.locked()) lock.release() self.assertFalse(lock.locked())
def test_release_not_acquired(self): lock = asyncio.Lock(loop=self.loop) self.assertRaises(RuntimeError, lock.release)
def test_release_no_waiters(self): lock = asyncio.Lock(loop=self.loop) self.loop.run_until_complete(lock.acquire()) self.assertTrue(lock.locked()) lock.release() self.assertFalse(lock.locked())
def test_context_manager(self): lock = asyncio.Lock(loop=self.loop) @asyncio.coroutine def acquire_lock(): return (yield from lock) with self.loop.run_until_complete(acquire_lock()): self.assertTrue(lock.locked()) self.assertFalse(lock.locked())
def test_explicit_lock(self): lock = asyncio.Lock(loop=self.loop) cond = asyncio.Condition(lock, loop=self.loop) self.assertIs(cond._lock, lock) self.assertIs(cond._loop, lock._loop)
def test_ambiguous_loops(self): loop = self.new_test_loop() self.addCleanup(loop.close) lock = asyncio.Lock(loop=self.loop) with self.assertRaises(ValueError): asyncio.Condition(lock, loop=loop)
def main(loop): lock = asyncio.Lock() await asyncio.wait([myWorker(lock), myWorker(lock)]),
def __init__(self, *, selector=None, loop=None): super().__init__(loop=loop) # use the custom asyncio task factory self._loop.set_task_factory(tukio_factory) self._selector = selector or _WorkflowSelector() self._instances = [] self._broker = get_broker(self._loop) self._lock = asyncio.Lock() self._must_stop = False
def __init__(self, wf_tmpl, *, loop=None, broker=None): super().__init__(loop=loop) self.uid = str(uuid4()) self._template = wf_tmpl # Start and end datetime (UTC) of the execution of the workflow self._start, self._end = None, None # Set of tasks executed at some point. Items of that set are # instances of `asyncio.Task` self.tasks = set() self._tasks_by_id = dict() # This dict references all tasks that updated the set of their # downstream tasks at runtime. Keys are `asyncio.Task` objects and # values are sets of task template IDs. self._updated_next_tasks = dict() self._done_tasks = set() self._internal_exc = None self._must_cancel = False self.lock = asyncio.Lock() # Create the workflow in the 'locked' state when its overrun policy is # 'skip-until-unlock'. if self.policy is OverrunPolicy.skip_until_unlock: self.lock._locked = True # Work with an event broker self._broker = broker or get_broker(self._loop) self._source = EventSource( workflow_template_id=self._template.uid, workflow_exec_id=self.uid ) # A 'committed' workflow is a pending workflow not suspended self._committed = asyncio.Event() self._timed_out = False
def __init__(self, sender_id, api_key, max_connections=10, loop=None): self.sender_id = sender_id self.api_key = api_key self.max_connections = max_connections self.loop = loop or asyncio.get_event_loop() self.connections = [] self._lock = asyncio.Lock() self.loop.set_exception_handler(self.exception_handler)
def __init__(self, loop: asyncio.AbstractEventLoop, future_store: FutureStore, channel: Channel, name, durable, exclusive, auto_delete, arguments): super().__init__(loop, future_store) self._channel = channel self.name = name or '' self.durable = durable self.exclusive = exclusive self.auto_delete = auto_delete self.arguments = arguments self.declaration_result = None # type: DeclarationResult self._get_lock = asyncio.Lock(loop=self.loop)
def __init__(self, connection, loop: asyncio.AbstractEventLoop, future_store: FutureStore, channel_number: int=None, publisher_confirms: bool=True, on_return_raises=False): """ :param connection: :class:`aio_pika.adapter.AsyncioConnection` instance :param loop: Event loop (:func:`asyncio.get_event_loop()` when :class:`None`) :param future_store: :class:`aio_pika.common.FutureStore` instance :param publisher_confirms: False if you don't need delivery confirmations (in pursuit of performance) """ super().__init__(loop, future_store.get_child()) self._channel = None # type: pika.channel.Channel self._connection = connection self._confirmations = {} self._on_return_callbacks = [] self._delivery_tag = 0 self._write_lock = asyncio.Lock(loop=self.loop) self._channel_number = channel_number self._publisher_confirms = publisher_confirms if not publisher_confirms and on_return_raises: raise RuntimeError('on_return_raises must be uses with publisher confirms') self._on_return_raises = on_return_raises self.default_exchange = self.EXCHANGE_CLASS( self._channel, self._publish, '', ExchangeType.DIRECT, durable=None, auto_delete=None, internal=None, passive=None, arguments=None, loop=self.loop, future_store=self._futures.get_child(), )
def __init__(self, name, **options): self.name = name self.object_hook = options.pop('object_hook', None) self.encoder = options.pop('encoder', None) self.loop = options.pop('loop', asyncio.get_event_loop()) self.lock = asyncio.Lock() if options.pop('load_later', False): self.loop.create_task(self.load()) else: self.load_from_file()