Python asyncio.Lock() Examples
The following are 30
code examples of asyncio.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
asyncio
, or try the search function
.
Example #1
Source File: help_channels.py From bot with MIT License | 7 votes |
def __init__(self, bot: Bot): super().__init__() self.bot = bot # Categories self.available_category: discord.CategoryChannel = None self.in_use_category: discord.CategoryChannel = None self.dormant_category: discord.CategoryChannel = None # Queues self.channel_queue: asyncio.Queue[discord.TextChannel] = None self.name_queue: t.Deque[str] = None self.name_positions = self.get_names() self.last_notification: t.Optional[datetime] = None # Asyncio stuff self.queue_tasks: t.List[asyncio.Task] = [] self.ready = asyncio.Event() self.on_message_lock = asyncio.Lock() self.init_task = self.bot.loop.create_task(self.init_cog())
Example #2
Source File: main.py From watchgod with MIT License | 6 votes |
def __init__(self, path: Union[Path, str], *, watcher_cls: Type[AllWatcher] = DefaultWatcher, watcher_kwargs: Optional[Dict[str, Any]] = None, debounce=1600, normal_sleep=400, min_sleep=50, stop_event: asyncio.Event = None, loop=None): self._loop = loop or asyncio.get_event_loop() self._executor = ThreadPoolExecutor(max_workers=4) self._path = path self._watcher_cls = watcher_cls self._watcher_kwargs = watcher_kwargs or dict() self._debounce = debounce self._normal_sleep = normal_sleep self._min_sleep = min_sleep self._stop_event = stop_event self._w = None asyncio.set_event_loop(self._loop) self.lock = asyncio.Lock()
Example #3
Source File: state.py From tattle with Mozilla Public License 2.0 | 6 votes |
def __init__(self, config, queue, events, loop=None): """ Initialize instance of the NodeManager class :param config: config object :param queue: broadcast queue :type config: tattle.config.Configuration :type events: tattle.event.EventManager :type queue: tattle.queue.BroadcastQueue """ self.config = config self._queue = queue self._events = events self._loop = loop or asyncio.get_event_loop() self._leaving = False self._nodes = list() self._nodes_map = dict() self._nodes_lock = asyncio.Lock() self._suspect_nodes = dict() self._local_node_name = None self._local_node_seq = sequence.Sequence()
Example #4
Source File: service.py From pyquarkchain with MIT License | 6 votes |
def __init__( self, token: CancelToken = None, loop: asyncio.AbstractEventLoop = None ) -> None: self.events = ServiceEvents() self._run_lock = asyncio.Lock() self._child_services = WeakSet() self._tasks = WeakSet() self._finished_callbacks = [] self._loop = loop base_token = CancelToken(type(self).__name__, loop=loop) if token is None: self.cancel_token = base_token else: self.cancel_token = base_token.chain(token)
Example #5
Source File: botchecker.py From BotListBot with MIT License | 6 votes |
def __init__(self, event_loop, session_name, api_id, api_hash, phone_number, workdir=None): self.event_loop = event_loop self.username_flood_until = None self._message_intervals = {} self._last_ping = None self.__photos_lock = asyncio.Lock(loop=self.event_loop) super(BotChecker, self).__init__( session_name, api_id, api_hash, workers=4, phone_number=phone_number, workdir=workdir ) self.logger.setLevel(logging.WARNING)
Example #6
Source File: protocol.py From aioh2 with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #7
Source File: state_delta_subscription_handler.py From sawtooth-core with Apache License 2.0 | 6 votes |
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)
Example #8
Source File: connection.py From aioapns with Apache License 2.0 | 6 votes |
def __init__(self, topic: Optional[str] = None, max_connections: int = 10, max_connection_attempts: Optional[int] = None, loop: Optional[asyncio.AbstractEventLoop] = None, use_sandbox: bool = False): self.apns_topic = topic self.max_connections = max_connections if use_sandbox: self.protocol_class = APNsDevelopmentClientProtocol else: self.protocol_class = APNsProductionClientProtocol self.loop = loop or asyncio.get_event_loop() self.connections = [] self._lock = asyncio.Lock(loop=self.loop) self.max_connection_attempts = max_connection_attempts
Example #9
Source File: slot_timer.py From loopchain with Apache License 2.0 | 6 votes |
def __init__(self, timer_key, duration, timer_service: TimerService, callback, callback_lock: asyncio.Lock, loop, call_instantly: bool): self.__slot = 0 self.__delayed = True self.__timer_key = timer_key self.__timer_service = timer_service self.__duration = duration self.__callback = callback self.__callback_lock = callback_lock self.__loop = loop self.is_running = False if call_instantly: self.call = self.call_instantly else: self.call = self.call_in_slot
Example #10
Source File: camera.py From homeassistant-config with The Unlicense | 6 votes |
def __init__(self, hass, amcrest): """Initialize an Amcrest camera.""" super(AmcrestCam, self).__init__() self._name = amcrest.name self._camera = amcrest.device self._ffmpeg = hass.data[DATA_FFMPEG] self._ffmpeg_arguments = amcrest.ffmpeg_arguments self._stream_source = amcrest.stream_source self._resolution = amcrest.resolution self._token = self._auth = amcrest.authentication self._is_recording = False self._motion_detection_enabled = None self._model = None self._audio_enabled = None self._motion_recording_enabled = None self._color_bw = None self._snapshot_lock = asyncio.Lock()
Example #11
Source File: rds.py From ScoutSuite with GNU General Public License v2.0 | 6 votes |
def _cache_instances(self, region: str): async with self._regional_instances_cache_locks.setdefault(region, Lock()): if region in self._instances_cache: return self._instances_cache[region] = await AWSFacadeUtils.get_all_pages( 'rds', region, self.session, 'describe_db_instances', 'DBInstances') for instance in self._instances_cache[region]: instance['VpcId'] = instance['DBSubnetGroup']['VpcId'] \ if 'DBSubnetGroup' in instance and 'VpcId' in instance['DBSubnetGroup'] \ and instance['DBSubnetGroup']['VpcId'] \ else ec2_classic await get_and_set_concurrently( [self._get_and_set_instance_clusters, self._get_and_set_instance_tags], self._instances_cache[region], region=region)
Example #12
Source File: elbv2.py From ScoutSuite with GNU General Public License v2.0 | 6 votes |
def cache_load_balancers(self, region): async with self.regional_load_balancers_cache_locks.setdefault(region, asyncio.Lock()): if region in self.load_balancers_cache: return self.load_balancers_cache[region] = \ await AWSFacadeUtils.get_all_pages('elbv2', region, self.session, 'describe_load_balancers', 'LoadBalancers') for load_balancer in self.load_balancers_cache[region]: load_balancer['VpcId'] = \ load_balancer['VpcId'] if 'VpcId' in load_balancer and load_balancer['VpcId'] else ec2_classic await get_and_set_concurrently( [self._get_and_set_load_balancer_attributes], self.load_balancers_cache[region], region=region) await get_and_set_concurrently( [self._get_and_set_load_balancer_tags], self.load_balancers_cache[region], region=region)
Example #13
Source File: connection_async.py From azure-uamqp-python with MIT License | 6 votes |
def __init__(self, hostname, sasl, container_id=False, max_frame_size=None, channel_max=None, idle_timeout=None, properties=None, remote_idle_timeout_empty_frame_send_ratio=None, error_policy=None, debug=False, encoding='UTF-8', loop=None): self.loop = loop or get_running_loop() super(ConnectionAsync, self).__init__( hostname, sasl, container_id=container_id, max_frame_size=max_frame_size, channel_max=channel_max, idle_timeout=idle_timeout, properties=properties, remote_idle_timeout_empty_frame_send_ratio=remote_idle_timeout_empty_frame_send_ratio, error_policy=error_policy, debug=debug, encoding=encoding) self._async_lock = asyncio.Lock(loop=self.loop)
Example #14
Source File: core.py From p2p-python with MIT License | 6 votes |
def __init__(self, host=None, listen=15): assert V.DATA_PATH is not None, 'Setup p2p params before CoreClass init.' assert host is None or host == 'localhost' # status params self.f_stop = False self.f_finish = False self.f_running = False # working info self.start_time = int(time()) self.number = 0 self.user: List[User] = list() self.user_lock = asyncio.Lock() self.host = host # local=>'localhost', 'global'=>None self.core_que = asyncio.Queue() self.backlog = listen self.traffic = Traffic() self.ping_status: Dict[int, asyncio.Event] = ExpiringDict(max_len=5000, max_age_seconds=900)
Example #15
Source File: paradox.py From pai with Eclipse Public License 2.0 | 6 votes |
def __init__(self, retries=3): self.panel = None # type: Panel self._connection = None self.retries = retries self.work_loop = asyncio.get_event_loop() # type: asyncio.AbstractEventLoop self.work_loop.set_exception_handler(async_loop_unhandled_exception_handler) self.receive_worker_task = None self.storage = Storage() self._run_state = RunState.STOP self.request_lock = asyncio.Lock() self.busy = asyncio.Lock() self.loop_wait_event = asyncio.Event() ps.subscribe(self._on_labels_load, "labels_loaded") ps.subscribe(self._on_definitions_load, "definitons_loaded") ps.subscribe(self._on_status_update, "status_update") ps.subscribe(self._on_event, "events") ps.subscribe(self._on_property_change, "changes")
Example #16
Source File: scanner.py From telegram-uz-bot with MIT License | 6 votes |
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
Example #17
Source File: files.py From aiologger with MIT License | 6 votes |
def __init__( self, filename: str, mode: str = "a", encoding: str = None, namer: Namer = None, rotator: Rotator = None, *, loop: Optional[AbstractEventLoop] = None, ) -> None: super().__init__(filename, mode, encoding, loop=loop) self.mode = mode self.encoding = encoding self.namer = namer self.rotator = rotator self._rollover_lock: Optional[asyncio.Lock] = None
Example #18
Source File: files.py From aiologger with MIT License | 6 votes |
def emit(self, record: LogRecord): # type: ignore """ Emit a record. Output the record to the file, catering for rollover as described in `do_rollover`. """ try: if self.should_rollover(record): if not self._rollover_lock: self._rollover_lock = asyncio.Lock(loop=self.loop) async with self._rollover_lock: if self.should_rollover(record): await self.do_rollover() await super().emit(record) except Exception as exc: await self.handle_error(record, exc)
Example #19
Source File: extending.py From tekore with MIT License | 6 votes |
def _async_send(self, request: Request): if request.method.lower() != 'get': return await self.sender.send(request) if self._lock is None: self._lock = asyncio.Lock() async with self._lock: cached, etag = self._load(request) if cached is not None and etag is None: return cached elif etag is not None: request.headers.update(ETag=etag) fresh = await self.sender.send(request) async with self._lock: return self._handle_fresh(request, fresh, cached)
Example #20
Source File: streams.py From aiologger with MIT License | 6 votes |
def __init__( self, stream=None, level: Union[str, int, LogLevel] = LogLevel.NOTSET, formatter: Formatter = None, filter: Filter = None, *, loop: Optional[AbstractEventLoop] = None, ) -> None: super().__init__(loop=loop) if stream is None: stream = sys.stderr self.stream = stream self.level = level if formatter is None: formatter = Formatter() self.formatter: Formatter = formatter if filter: self.add_filter(filter) self.protocol_class = AiologgerProtocol self._initialization_lock = asyncio.Lock(loop=self.loop) self.writer: Optional[StreamWriter] = None
Example #21
Source File: utils.py From lrrbot with Apache License 2.0 | 6 votes |
def __init__(self, period=DEFAULT_THROTTLE, params=[], log=True, count=1): self.period = period self.watchparams = params self.lastrun = {} self.lastreturn = {} self.lastcleanup = time.time() self.log = log self.count = count self.lock = asyncio.Lock() # need to decorate this here, rather than putting a decorator on the actual # function, as it needs to wrap the *bound* method, so there's no "self" # parameter. Meanwhile, we're wrapping this "decorate" function instead of # just wrapping __call__ as setting __call__ directly on instances doesn't # work, Python gets the __call__ function from the class, not individual # instances. self.decorate = coro_decorator(self.decorate)
Example #22
Source File: punish.py From calebj-cogs with GNU General Public License v3.0 | 6 votes |
def __init__(self, bot): self.bot = bot self.json = compat_load(JSON) # queue variables self.queue = asyncio.PriorityQueue(loop=bot.loop) self.queue_lock = asyncio.Lock(loop=bot.loop) self.pending = {} self.enqueued = set() try: self.analytics = CogAnalytics(self) except Exception as error: self.bot.logger.exception(error) self.analytics = None self.task = bot.loop.create_task(self.on_load())
Example #23
Source File: elb.py From ScoutSuite with GNU General Public License v2.0 | 6 votes |
def cache_load_balancers(self, region): async with self.regional_load_balancers_cache_locks.setdefault(region, asyncio.Lock()): if region in self.load_balancers_cache: return self.load_balancers_cache[region] = \ await AWSFacadeUtils.get_all_pages('elb', region, self.session, 'describe_load_balancers', 'LoadBalancerDescriptions') for load_balancer in self.load_balancers_cache[region]: load_balancer['VpcId'] = \ load_balancer['VPCId'] if 'VPCId' in load_balancer and load_balancer['VPCId'] else ec2_classic await get_and_set_concurrently( [self._get_and_set_load_balancer_attributes], self.load_balancers_cache[region], region=region) await get_and_set_concurrently( [self._get_and_set_load_balancer_tags], self.load_balancers_cache[region], region=region)
Example #24
Source File: decorator.py From thenextquant with MIT License | 5 votes |
def async_method_locker(name, wait=True): """ 异步方法加锁,用于多个协程执行同一个单列的函数时候,避免共享内存相互修改 @param name 锁名称 @param wait 如果被锁是否等待,True等待执行完成再返回,False不等待直接返回 * NOTE: 此装饰器需要加到async异步方法上 """ assert isinstance(name, str) def decorating_function(method): global METHOD_LOCKERS locker = METHOD_LOCKERS.get(name) if not locker: locker = asyncio.Lock() METHOD_LOCKERS[name] = locker @functools.wraps(method) async def wrapper(*args, **kwargs): if not wait and locker.locked(): return try: await locker.acquire() return await method(*args, **kwargs) finally: locker.release() return wrapper return decorating_function # class Test: # # @async_method_locker('my_fucker', False) # async def test(self, x): # print('hahaha ...', x) # await asyncio.sleep(0.1) # # # t = Test() # for i in range(10): # asyncio.get_event_loop().create_task(t.test(i)) # # asyncio.get_event_loop().run_forever()
Example #25
Source File: producer.py From aioamqp_consumer with MIT License | 5 votes |
def _init_ensure_locks(self): self._ensure_queue_lock = asyncio.Lock() self._ensure_exchange_lock = asyncio.Lock() self._ensure_bind_queue_lock = asyncio.Lock()
Example #26
Source File: client.py From telegram-uz-bot with MIT License | 5 votes |
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
Example #27
Source File: stats_publisher.py From indy-plenum with Apache License 2.0 | 5 votes |
def __init__(self, destIp, destPort, msg_buff_max_size): self.ip = destIp self.port = destPort self.msg_buff_max_size = msg_buff_max_size self._reader = None self._writer = None self._messageBuffer = deque() self._loop = asyncio.get_event_loop() self._connectionSem = asyncio.Lock()
Example #28
Source File: player_network_interface.py From poke-env with MIT License | 5 votes |
def __init__( self, player_configuration: PlayerConfiguration, *, avatar: Optional[int] = None, log_level: Optional[int] = None, server_configuration: ServerConfiguration, start_listening: bool = True, ) -> None: """ :param player_configuration: Player configuration. :type player_configuration: PlayerConfiguration :param avatar: Player avatar id. Optional. :type avatar: int, optional :param log_level: The player's logger level. :type log_level: int. Defaults to logging's default level. :param server_configuration: Server configuration. :type server_configuration: ServerConfiguration :param start_listening: Wheter to start listening to the server. Defaults to True. :type start_listening: bool """ self._authentication_url = server_configuration.authentication_url self._avatar = avatar self._password = player_configuration.password self._username = player_configuration.username self._server_url = server_configuration.server_url self._logged_in: Event = Event() self._sending_lock = Lock() self._websocket: websockets.client.WebSocketClientProtocol # pyre-ignore self._logger: Logger = self._create_player_logger(log_level) # pyre-ignore if start_listening: self._listening_coroutine = ensure_future(self.listen())
Example #29
Source File: decorator.py From aioquant with MIT License | 5 votes |
def async_method_locker(name, wait=True, timeout=1): """ In order to share memory between any asynchronous coroutine methods, we should use locker to lock our method, so that we can avoid some un-prediction actions. Args: name: Locker name. wait: If waiting to be executed when the locker is locked? if True, waiting until to be executed, else return immediately (do not execute). timeout: Timeout time to be locked, default is 1s. NOTE: This decorator must to be used on `async method`. """ assert isinstance(name, str) def decorating_function(method): global METHOD_LOCKERS locker = METHOD_LOCKERS.get(name) if not locker: locker = asyncio.Lock() METHOD_LOCKERS[name] = locker @functools.wraps(method) async def wrapper(*args, **kwargs): if not wait and locker.locked(): return try: await locker.acquire() return await asyncio.wait_for(method(*args, **kwargs), timeout) # return await method(*args, **kwargs) finally: locker.release() return wrapper return decorating_function
Example #30
Source File: indy.py From aries-cloudagent-python with Apache License 2.0 | 5 votes |
def __init__( self, pool_name: str, wallet: BaseWallet, *, keepalive: int = 0, cache: BaseCache = None, cache_duration: int = 600, read_only: bool = False, ): """ Initialize an IndyLedger instance. Args: pool_name: The Indy pool ledger configuration name wallet: IndyWallet instance keepalive: How many seconds to keep the ledger open cache: The cache instance to use cache_duration: The TTL for ledger cache entries """ self.logger = logging.getLogger(__name__) self.opened = False self.ref_count = 0 self.ref_lock = asyncio.Lock() self.keepalive = keepalive self.close_task: asyncio.Future = None self.cache = cache self.cache_duration = cache_duration self.wallet = wallet self.pool_handle = None self.pool_name = pool_name self.taa_acceptance = None self.taa_cache = None self.read_only = read_only if wallet.WALLET_TYPE != "indy": raise LedgerConfigError("Wallet type is not 'indy'")