我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用asyncio.TimeoutError()。
def _get_connection(target, port, ssl, timeout, loop): con = asyncio.open_connection(host=target, port=port, ssl=ssl) try: reader, writer = await asyncio.wait_for(con, int(timeout), loop=loop) return reader, writer except (asyncio.TimeoutError, ConnectionRefusedError): # If this is reach -> port closed return None, None
def test_single_proxy(self, proxy): """ text one proxy, if valid, put them to usable_proxies. """ try: async with aiohttp.ClientSession() as session: try: if isinstance(proxy, bytes): proxy = proxy.decode('utf-8') real_proxy = 'http://' + proxy print('Testing', proxy) async with session.get(self.test_api, proxy=real_proxy, timeout=get_proxy_timeout) as response: if response.status == 200: self._conn.put(proxy) print('Valid proxy', proxy) except (ProxyConnectionError, TimeoutError, ValueError): print('Invalid proxy', proxy) except (ServerDisconnectedError, ClientResponseError,ClientConnectorError) as s: print(s) pass
def test_limit(self): await self.plugin.before_request(self.endpoint_desc, self.session, self.request_params) fut = asyncio.ensure_future(self.plugin.before_request(self.endpoint_desc, self.session, self.request_params)) with self.assertRaises(TimeoutError): await asyncio.wait_for(shield(fut), 0.1) await self.plugin.on_response(self.endpoint_desc, self.session, self.request_params, None) await asyncio.sleep(0.2) await asyncio.wait_for(fut, 0.5)
def test_limit_using_exception(self): await self.plugin.before_request(self.endpoint_desc, self.session, self.request_params) fut = asyncio.ensure_future(self.plugin.before_request(self.endpoint_desc, self.session, self.request_params)) with self.assertRaises(TimeoutError): await asyncio.wait_for(shield(fut), 0.1) await self.plugin.on_exception(self.endpoint_desc, self.session, self.request_params, Exception()) await asyncio.sleep(0.2) await asyncio.wait_for(fut, 0.1)
def testing(label, cache, loop): def w(g): return asyncio.wait_for(g, args.timeout, loop=loop) key = 'foo-%s' % label while True: logging.info('%s %s', label, '-'*20) try: ret = yield from w(cache.set(key, 'hello-%s-world' % label)) logging.info('%s set %s', label, ret) ret = yield from w(cache.get(key)) logging.info('%s get %s', label, ret) ret = yield from w(cache.delete(key)) logging.info('%s del %s', label, ret) ret = yield from w(cache.get(key)) logging.info('%s get2 %s', label, ret) except asyncio.TimeoutError: logging.warn('%s Timeout', label) except Exception as exc: logging.exception('%s Client exception: %r', label, exc) break
def test_sock_connect_timeout(self): # Tulip issue #205: sock_connect() must unregister the socket on # timeout error # prepare mocks self.loop.add_writer = mock.Mock() self.loop.remove_writer = mock.Mock() sock = test_utils.mock_nonblocking_socket() sock.connect.side_effect = BlockingIOError # first call to sock_connect() registers the socket fut = self.loop.sock_connect(sock, ('127.0.0.1', 80)) self.assertTrue(sock.connect.called) self.assertTrue(self.loop.add_writer.called) self.assertEqual(len(fut._callbacks), 1) # on timeout, the socket must be unregistered sock.connect.reset_mock() fut.set_exception(asyncio.TimeoutError) with self.assertRaises(asyncio.TimeoutError): self.loop.run_until_complete(fut) self.assertTrue(self.loop.remove_writer.called)
def test_create_connection_timeout(self, m_socket): # Ensure that the socket is closed on timeout sock = mock.Mock() m_socket.socket.return_value = sock def getaddrinfo(*args, **kw): fut = asyncio.Future(loop=self.loop) addr = (socket.AF_INET, socket.SOCK_STREAM, 0, '', ('127.0.0.1', 80)) fut.set_result([addr]) return fut self.loop.getaddrinfo = getaddrinfo with mock.patch.object(self.loop, 'sock_connect', side_effect=asyncio.TimeoutError): coro = self.loop.create_connection(MyProto, '127.0.0.1', 80) with self.assertRaises(asyncio.TimeoutError): self.loop.run_until_complete(coro) self.assertTrue(sock.close.called)
def update(self, *args, **kwargs): guilds = len(self.bot.guilds) if self.carbon_key is not None: carbon_payload = {'key': self.carbon_key, 'servercount': guilds} try: resp = await self.bot.request(CARBON_API, data=carbon_payload, type_='text') except asyncio.TimeoutError: logging.info(f'Carbon failed to respond for\n{json.dumps(carbon_payload, indent=2)}') else: logging.info(f'Carbon returned {resp.status} for\n{json.dumps(carbon_payload, indent=2)}') for site, link, key in (('DBots.pw', DBOTSPW_API, self.dbotspw_key), ('DBots.org', DBOTSORG_API, self.dbotsorg_key)): if key is not None: dbots_payload = {'server_count': guilds} dbots_headers = {'authorization': key, 'content-type': 'application/json'} try: resp = await self.bot.request(link.format(self.bot), data=dbots_payload, headers=dbots_headers, type_='text') except asyncio.TimeoutError: logging.info(f'{site} failed to respond for\n{json.dumps(dbots_payload, indent=2)}') else: logging.info(f'{site} returned {resp.status} for\n{json.dumps(dbots_payload, indent=2)}')
def async_unsubscribe_all(self): """ Disconnect from device. This removes all UpnpServices. """ _LOGGER.debug('%s.async_disconnect()', self) if not self._device: return for service in self._device.services.values(): try: sid = service.subscription_sid if sid: self._callback_view.unregister_service(sid) yield from service.async_unsubscribe(True) except (asyncio.TimeoutError, aiohttp.ClientError): pass
def expect_async(expecter, timeout=None): # First process data that was previously read - if it maches, we don't need # async stuff. previously_read = expecter.spawn.buffer expecter.spawn.buffer = expecter.spawn.string_type() idx = expecter.new_data(previously_read) if idx is not None: return idx transport, pw = yield from asyncio.get_event_loop()\ .connect_read_pipe(lambda: PatternWaiter(expecter), expecter.spawn) try: return (yield from asyncio.wait_for(pw.fut, timeout)) except asyncio.TimeoutError as e: transport.pause_reading() return expecter.timeout(e)
def close(self, timeout=5): """Stop a ffmpeg instance.""" if not self.is_running: _LOGGER.warning("FFmpeg isn't running!") return try: # send stop to ffmpeg with async_timeout.timeout(timeout, loop=self._loop): yield from self._proc.communicate(input=b'q') _LOGGER.debug("Close FFmpeg process") except (asyncio.TimeoutError, ValueError): _LOGGER.warning("Timeout while waiting of FFmpeg") self._proc.kill() finally: self._clear()
def wait_cgroup(sock, execute_task, time_limit_ns, memory_limit_bytes, process_limit): cgroup = CGroup() try: cgroup.memory_limit_bytes = memory_limit_bytes cgroup.pids_max = process_limit await cgroup.accept(sock) start_idle = _get_idle() while True: cpu_usage_ns = cgroup.cpu_usage_ns idle_usage_ns = int((_get_idle() - start_idle) / cpu_count() * 1e9) time_usage_ns = max(cpu_usage_ns, idle_usage_ns) time_remain_ns = time_limit_ns - time_usage_ns if time_remain_ns <= 0: return time_usage_ns, cgroup.memory_usage_bytes try: await wait_for(shield(execute_task), (time_remain_ns + WAIT_JITTER_NS) / 1e9) return cgroup.cpu_usage_ns, cgroup.memory_usage_bytes except TimeoutError: pass finally: while cgroup.kill(): await sleep(.001) cgroup.close()
def run(self): """ Runs the async partion reciever event loop to retrive messages from the event queue """ # Implement pull max batch from queue instead of one message at a time while (not self.eh_partition_pump.is_closing()) \ or self.eh_partition_pump.pump_status == "Errored": try: if self.eh_partition_pump.partition_receive_handler: msgs = await asyncio.wait_for(self.eh_partition_pump.\ partition_receive_handler. \ receive(self.max_batch_size), self.recieve_timeout, loop=self.eh_partition_pump.loop) await self.process_events_async(msgs) except asyncio.TimeoutError as err: if self.eh_partition_pump.partition_receive_handler: logging.info("No events received, queue size %d, delivered %d", self.eh_partition_pump.partition_receive_handler.messages.qsize(), self.eh_partition_pump.partition_receive_handler.delivered) if self.eh_partition_pump.host.eph_options.release_pump_on_timeout: await self.process_error_async(err)
def pump(_pid, _recv, _dl): total = 0 iteration = 0 while time.time() < _dl: try: batch = await asyncio.wait_for(_recv.receive(100), 60.0) size = len(batch) total += size iteration += size if iteration >= 80: iteration = 0 logger.info("%s: total received %d, last sn=%d, last offset=%s", _pid, total, batch[-1].sequence_number, batch[-1].offset) except asyncio.TimeoutError: logger.info("%s: No events received, queue size %d, delivered %d", _pid, _recv.messages.qsize(), _recv.delivered)
def ws_connect(self): """Open a websocket connection for real time events.""" if self.ws_connected: raise exceptions.TransportError('Connection already open.') _LOGGER.info("Opening websocket connection.") try: # Open an engineIO session session_data = yield from self._get_engineio_session() # Now that the session data has been fetched, open the actual # websocket connection. ws_connection = yield from self._get_ws_connection(session_data) # Finalize connection status self._ws_connection = ws_connection self._ws_session_data = session_data # Send the first ping packet self.loop.create_task(self._ping()) except (ClientError, HttpProcessingError, asyncio.TimeoutError) as exc: raise exceptions.TransportError from exc return self.loop.create_task(self._ws_loop())
def _ws_loop(self): """Run the websocket loop listening for messages.""" msg = None try: while True: msg = yield from self._ws_connection.receive() if msg.type == aiohttp.WSMsgType.TEXT: self._handle_packet(msg.data) elif msg.type == aiohttp.WSMsgType.CLOSED: break elif msg.type == aiohttp.WSMsgType.ERROR: break except (ClientError, HttpProcessingError, asyncio.TimeoutError) as exc: raise exceptions.TransportError from exc finally: yield from self.ws_close() self._handle_event(EVENT_WS_CLOSED, None) if msg is not None and msg.type == aiohttp.WSMsgType.ERROR: raise exceptions.TransportError( 'Websocket error detected. Connection closed.')
def ws_close(self): """Close the websocket connection.""" if not self.ws_connected: return # Try to gracefully end the connection try: yield from self._ws_connection.send_str('41') yield from self._ws_connection.send_str('1') except (ClientError, HttpProcessingError, asyncio.TimeoutError): pass # Close any remaining ping handles handle = self._ws_session_data.get(ATTR_PING_INTERVAL_HANDLE) if handle: handle.cancel() handle = self._ws_session_data.get(ATTR_PING_TIMEOUT_HANDLE) if handle: handle.cancel() yield from self._ws_connection.close() self._ws_connection = None self._ws_session_data = None
def retry_request(*args, retry_exceptions=(asyncio.TimeoutError, ScriptWorkerRetryException), retry_async_kwargs=None, **kwargs): """Retry the ``request`` function. Args: *args: the args to send to request() through retry_async(). retry_exceptions (list, optional): the exceptions to retry on. Defaults to (ScriptWorkerRetryException, ). retry_async_kwargs (dict, optional): the kwargs for retry_async. If None, use {}. Defaults to None. **kwargs: the kwargs to send to request() through retry_async(). Returns: object: the value from request(). """ retry_async_kwargs = retry_async_kwargs or {} return await retry_async(request, retry_exceptions=retry_exceptions, args=args, kwargs=kwargs, **retry_async_kwargs) # datestring_to_timestamp {{{1
def start_polling(self, wait_on_error=60): """Wrapper around self.poll_base_resource() for exception handling.""" while True: try: await self.poll_base_resource() except asyncio.CancelledError: self.logger.info('Polling cancelled') break except asyncio.TimeoutError: self.logger.warning('Polling failed due to TimeoutError') except (APIError, TimeoutError, ClientOSError, ClientResponseError) as e: # log error and start all over again self.logger.warning('Polling failed with a temporary error: {}'.format(e)) except: self.logger.exception('Polling failed with an unexpected exception:') self.action_id = None self.logger.info('Retry will happen in {} seconds'.format( wait_on_error)) await asyncio.sleep(wait_on_error)
def finish_connections(self, timeout=None): # try to close connections in 90% of graceful timeout timeout90 = None if timeout: timeout90 = timeout / 100 * 90 for handler in self._connections.keys(): handler.closing(timeout=timeout90) if timeout: try: await wait_for( self._connections_cleanup(), timeout, loop=self._loop) except TimeoutError: self._server_logger.warning( "Not all connections are closed (pending: %d)", len(self._connections)) for transport in self._connections.values(): transport.close() self._connections.clear()
def _start_loop(self, websocket, event_handler): """ We will listen for websockets events, sending a heartbeat/pong everytime we react a TimeoutError. If we don't the webserver would close the idle connection, forcing us to reconnect. """ log.debug('Starting websocket loop') while True: try: yield from asyncio.wait_for( self._wait_for_message(websocket, event_handler), timeout=self.options['timeout'] ) except asyncio.TimeoutError: yield from websocket.pong() log.debug("Sending heartbeat...") continue
def dad_jokes(message, match): name = ' '.join(match.group(5).replace('@everyone', '@\N{ZERO WIDTH SPACE}everyone').replace('@here', '@\N{ZERO WIDTH SPACE}here').split()[0:4]) if name.startswith((',', '.', '!')): name = name[1:].lstrip() for mem in message.mentions: name = name.replace(mem.mention, [sub for sub in [mem.display_name, mem.name, '<\\@%s>' % mem.id] if '<@' not in sub][0]) for role in message.role_mentions: name = name.replace(role.mention, role.name if '<@' not in role.name else '<\\@&%s>' % role.id) response = await message.channel.send('Hello **%s**, I\'m %s!' % (name, message.guild.me.display_name if isinstance(message.channel, discord.TextChannel) else message.channel.me.display_name)) try: await bot.wait_for('message_delete', check=lambda m: m == message, timeout=60) except asyncio.TimeoutError: pass else: await response.delete() ### LONGCAT
def quiz(self, ctx): if not self.cache_full: await self.fill_cache_command.callback(self, ctx) team = random.choice(list(self.team_cache.values())) if random.randrange(2): # Random bool; true is put the number, ask for the name query = 'What is the name of team %d?' % team.team_number answer = clean_name(team.nickname) else: query = 'What is the number of "%s"?' % team.nickname answer = str(team.team_number) try: for i in range(3, 0, -1): await ctx.send('%s (%d attempt(s) left)' % (query, i)) response = await ctx.bot.wait_for('message', check=lambda m: m.channel == ctx.channel and m.author == ctx.author, timeout=60) if clean_name(response.content) == answer: await ctx.send('Correct!') break elif i == 1: # Done await ctx.send('BAKA! The correct answer was %s.' % answer) else: await ctx.send('BAKA! Incorrect!') except asyncio.TimeoutError: await ctx.send('BAKA! Out of time!')
def __aiter__(self): self.message = await self.dest.send(embed=self.pages[self.page]) for emoji in self._reactions: await self.message.add_reaction(emoji) while True: try: reaction, reacting_member = await self.bot.wait_for('reaction_add', check=self._check_reaction, timeout=self.timeout) except asyncio.TimeoutError: break yield reaction.emoji # Caller calls methods to set self._action; end of async for block, control resumes here if self._remove_reactions: await self.message.remove_reaction(reaction.emoji, reacting_member) if self._action is self._stop_reaction: break elif self._action is None: pass else: await self._action for emoji in reversed(self._reactions): await self.message.remove_reaction(emoji, self.me)
def yes_no(ctx: commands.Context, message: str="Are you sure? Type **yes** within 10 seconds to confirm. o.o"): """Yes no helper. Ask a confirmation message with a timeout of 10 seconds. ctx - The context in which the question is being asked. message - Optional messsage that the question should ask. """ await ctx.send(message) try: message = await ctx.bot.wait_for("message", timeout=10, check=lambda message: message.author == ctx.message.author) except asyncio.TimeoutError: await ctx.send("Timed out waiting. :<") return False if message.clean_content.lower() not in ["yes", "y"]: await ctx.send("Command cancelled. :<") return False return True
def poll(self): """Wait for packets to send to the client.""" try: packets = [await asyncio.wait_for(self.queue.get(), self.server.ping_timeout)] self.queue.task_done() except (asyncio.TimeoutError, asyncio.CancelledError): raise IOError() if packets == [None]: return [] try: packets.append(self.queue.get_nowait()) self.queue.task_done() except asyncio.QueueEmpty: pass return packets
def afk_send(ctx, message_key, *args, **kwargs): global afk_targets if afk_targets is None: afk_targets = {channel.id: channel for channel in ctx.bot.get_all_channels() if isinstance(channel, discord.TextChannel)} afk_targets.update({mem.id: mem for mem in ctx.bot.get_all_members() if mem.bot}) for info in ctx.bot.config['afk_messages']: if message_key in info: trigger = await afk_targets[info['dest']].send(info[message_key].format(*args, **kwargs)) try: response = await ctx.bot.wait_for('message', check=lambda m: m.channel == trigger.channel, timeout=10) await response.ack() except asyncio.TimeoutError: pass await ctx.message.delete()
def wait_for_entrance(self, timeout=None, nick=None, build=None, addr=None, check=None): """ Waits for entrance. :param timeout: Time to wait for entrance event, if exceeded, returns None. :param nick: Player's nick. :param build: Player's build. :param addr: Player's address (IP:PORT) :return: """ future = asyncio.Future(loop=self.loop) margs = (nick, build, addr) predicate = self.__get_predicate(margs, check) self.__listeners[ServerEvent.ENTRANCE].append((predicate, future)) try: data = yield from asyncio.wait_for(future, timeout, loop=self.loop) except asyncio.TimeoutError: data = None return data
def wait_for_respawn(self, timeout=None, team=None, nick=None, check=None): """ Waits for respawn event. :param timeout: Time to wait for respawn event, if exceeded, returns None. :param team: Player's team. :param nick: Player's nick. :param check: Check function, ignored if none. :return: Returns message info dict keys: ('team', 'nick'). :rtype: dict """ future = asyncio.Future(loop=self.loop) margs = (team, nick) predicate = self.__get_predicate(margs, check) self.__listeners[ServerEvent.RESPAWN].append((predicate, future)) try: data = yield from asyncio.wait_for(future, timeout, loop=self.loop) except asyncio.TimeoutError: data = None return data
def wait_for_elim_teams_flag(self, timeout=None, team=None, nick=None, points=None, check=None): """ Waits for elim teams flag event. :param timeout: Time to wait for event, if exceeded, returns None. :param team: Player's team. :param nick: Player's nick. :param points: Points scored. :type points: int :param check: Check function, ignored if none. :return: Returns message info dict keys: ('team', 'nick', 'points'). :rtype: dict """ future = asyncio.Future(loop=self.loop) margs = (team, nick, points) predicate = self.__get_predicate(margs, check) self.__listeners[ServerEvent.ELIM_TEAMS_FLAG].append((predicate, future)) try: data = yield from asyncio.wait_for(future, timeout, loop=self.loop) except asyncio.TimeoutError: data = None return data
def wait_for_team_switched(self, timeout=None, nick=None, old_team=None, new_team=None, check=None): """ Waits for team switch event. :param timeout: Time to wait for event, if exceeded, returns None. :param old_team: Player's old team. :param new_team: Player's new team. :param nick: Player's nick. :param check: Check function, ignored if none. :return: Returns message info dict keys: ('nick', 'old_team', 'new_nick'). :rtype: dict """ future = asyncio.Future(loop=self.loop) margs = (nick, old_team, new_team) predicate = self.__get_predicate(margs, check) self.__listeners[ServerEvent.TEAM_SWITCHED].append((predicate, future)) try: data = yield from asyncio.wait_for(future, timeout, loop=self.loop) except asyncio.TimeoutError: data = None return data
def wait_for_round_started(self, timeout=None, check=None): """ Waits for round start. :param timeout: Time to wait for event, if exceeded, returns None. :param check: Check function, ignored if none. :return: Returns an empty dict. :rtype: dict """ future = asyncio.Future(loop=self.loop) margs = tuple() predicate = self.__get_predicate(margs, check) self.__listeners[ServerEvent.ROUND_STARTED].append((predicate, future)) try: data = yield from asyncio.wait_for(future, timeout, loop=self.loop) except asyncio.TimeoutError: data = None return data
def wait_for_game_end(self, timeout=None, score_blue=None, score_red=None, score_yellow=None, score_purple=None, check=None): """ Waits for game end. :param timeout: Time to wait for event, if exceeded, returns None. :param score_blue: Blue score :param score_red: Red score. :param score_yellow: Yellow score. :param score_purple: Purple score. :param check: Check function, ignored if none. :return: Returns an empty dict. :rtype: dict """ future = asyncio.Future(loop=self.loop) margs = (score_blue, score_red, score_yellow, score_purple) predicate = self.__get_predicate(margs, check) self.__listeners[ServerEvent.GAME_END].append((predicate, future)) try: data = yield from asyncio.wait_for(future, timeout, loop=self.loop) except asyncio.TimeoutError: data = None return data
def wait_for_elim(self, timeout=None, killer_nick=None, killer_weapon=None, victim_nick=None, victim_weapon=None, check=None): """ Waits for elimination event. :param timeout: Time to wait for elimination event, if exceeded, returns None. :param killer_nick: Killer's nick to match, ignored if None. :param killer_weapon: Killer's weapon to match, ignored if None. :param victim_nick: Victim's nick to match, ignored if None. :param victim_weapon: Victim's weapon to match, ignored if None. :param check: Check function, ignored if None. :return: Returns message info dict keys: ('killer_nick', 'killer_weapon', 'victim_nick', 'victim_weapon') :rtype: dict """ future = asyncio.Future(loop=self.loop) margs = (killer_nick, killer_weapon, victim_nick, victim_weapon) predicate = self.__get_predicate(margs, check) self.__listeners[ServerEvent.ELIM].append((predicate, future)) try: elim_info = yield from asyncio.wait_for(future, timeout, loop=self.loop) except asyncio.TimeoutError: elim_info = None return elim_info
def connect(self, remote: kademlia.Node) -> LESPeer: """ Connect to the given remote and return a Peer instance when successful. Returns None if the remote is unreachable, times out or is useless. """ if remote in self.connected_nodes: self.logger.debug("Skipping %s; already connected to it", remote) return None expected_exceptions = ( UnreachablePeer, asyncio.TimeoutError, PeerConnectionLost, UselessPeer, PeerDisconnected) try: self.logger.info("Connecting to %s...", remote) peer = await asyncio.wait_for( handshake(remote, self.privkey, self.peer_class, self.chaindb, self.network_id, self.msg_handler), HANDSHAKE_TIMEOUT) return cast(LESPeer, peer) except expected_exceptions as e: self.logger.info("Could not complete handshake with %s: %s", remote, repr(e)) except Exception: self.logger.warn("Unexpected error during auth/p2p handhsake with %s: %s", remote, traceback.format_exc()) return None
def wait_ping(self, remote: Node) -> bool: """Wait for a ping from the given remote. This coroutine adds a callback to ping_callbacks and yields control until that callback is called or a timeout (k_request_timeout) occurs. At that point it returns whether or not a ping was received from the given node. """ if remote in self.ping_callbacks: raise AlreadyWaiting( "There's another coroutine waiting for a ping packet from {}".format(remote)) event = asyncio.Event() self.ping_callbacks[remote] = event.set got_ping = False try: got_ping = await asyncio.wait_for(event.wait(), k_request_timeout) self.logger.debug('got expected ping from {}'.format(remote)) except asyncio.futures.TimeoutError: self.logger.debug('timed out waiting for ping from {}'.format(remote)) # TODO: Use a contextmanager to ensure we always delete the callback from the list. del self.ping_callbacks[remote] return got_ping
def fetch(retry=0): proxy = 'http://{}'.format(Proxy.get_random()['address']) headers = {'user-agent': get_user_agent()} conn = aiohttp.ProxyConnector(proxy=proxy) url = 'http://httpbin.org/ip' try: with aiohttp.ClientSession(connector=conn) as session: with aiohttp.Timeout(TIMEOUT): async with session.get(url, headers=headers) as resp: return await resp.json() except (ProxyConnectionError, TimeoutError): try: p = Proxy.objects.get(address=proxy) if p: p.delete() except DoesNotExist: pass retry += 1 if retry > 5: raise TimeoutError() await asyncio.sleep(1) return await fetch(retry=retry)
def test_single_proxy(self, proxy): """ ?????? :param proxy: :return: """ conn = aiohttp.TCPConnector(verify_ssl=False) async with aiohttp.ClientSession(connector=conn) as session: try: if isinstance(proxy, bytes): proxy = proxy.decode('utf-8') real_proxy = 'http://' + proxy print('????', proxy) async with session.get(TEST_URL, proxy=real_proxy, timeout=15, allow_redirects=False) as response: if response.status in VALID_STATUS_CODES: self.redis.max(proxy) print('????', proxy) else: self.redis.decrease(proxy) print('???????? ', response.status, 'IP', proxy) except (ClientError, aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError, AttributeError): self.redis.decrease(proxy) print('??????', proxy)
def test_setup_prompt_timeout(self): device = self.mock_device("test-dev-1") devinfo = await self.mocks.device_db.get(device) self.options['open_timeout'] = 2 self.mock_options['prompt_delay'] = 3 handler = mock.Mock() session = MockCommandSession(self.mock_options, handler, devinfo, self.options, loop=self._loop) # Session is initially not connected self.assertFalse(session._connected) # Initiate the connection and wait for connect with self.assertRaises(asyncio.TimeoutError): await session.setup()
def test_setup_command_timeout(self): device = self.mock_device("test-dev-1") devinfo = await self.mocks.device_db.get(device) self.options['open_timeout'] = 2 # force a delay in setup commands self.mock_options['command_delay'] = 3 handler = mock.Mock() session = MockCommandSession(self.mock_options, handler, devinfo, self.options, loop=self._loop) # Session is initially not connected self.assertFalse(session._connected) # Initiate the connection and wait for connect with self.assertRaises(asyncio.TimeoutError): await session.setup()
def wait_on_sibling(self, sibling, time_limit=None): log.debug("Waiting on sibling %s", sibling) path = self.sibling_path(sibling) unblocked = self.client.wait_for_event(WatchEvent.DELETED, path) exists = await self.client.exists(path=path, watch=True) if not exists: unblocked.set_result(None) try: if time_limit: await asyncio.wait_for(unblocked, time_limit, loop=self.client.loop.create_future()) else: await unblocked except asyncio.TimeoutError: raise exc.TimeoutError
def wait(self, timeout=None): time_limit = None if timeout is not None: time_limit = time.time() + timeout barrier_lifted = self.client.wait_for_event( WatchEvent.DELETED, self.path ) exists = await self.client.exists(path=self.path, watch=True) if not exists: return try: if time_limit: await asyncio.wait_for(barrier_lifted, time_limit, loop=self.client.loop) else: await barrier_lifted except asyncio.TimeoutError: raise exc.TimeoutError
def close(self, timeout): if self.closing: return if self.pending or (self.pending_specials and self.pending_specials != {None: []}): log.warning('Pendings: {}; specials: {}'.format(self.pending, self.pending_specials)) self.closing = True try: # await list(pending_with_timeouts) self.abort(exception=exc.TimeoutError) # wlist = list(self.drain_all_pending()) # log.warn('Wait for list: {} {}'.format(wlist, self.pending)) # if len(wlist) > 0: # await asyncio.wait(wlist, timeout=timeout) except asyncio.TimeoutError: log.warn('ABORT Timeout') await self.abort(exception=exc.TimeoutError) except Exception as e: log.exception('in close: {}'.format(e)) raise e finally: log.debug('Closing writer') self.writer.close() log.debug('Writer closed')
def on_run(self): try: while True: await asyncio.wait_for( self.revive_event.wait(), timeout=self.timeout, loop=self.loop, ) self.revive_event.clear() except asyncio.TimeoutError: try: if asyncio.iscoroutinefunction(self.callback): await self.callback() else: self.callback() except Exception: logger.exception("Error in timeout callback execution.")
def on_run(self): while True: try: await asyncio.wait_for( self.reset_event.wait(), timeout=self.period, loop=self.loop, ) except asyncio.TimeoutError: try: if asyncio.iscoroutinefunction(self.callback): await self.callback() else: self.callback() except Exception: logger.exception("Error in timer callback execution.") else: self.reset_event.clear()
def test_ip_deny(event_loop): async with azmq.Context() as context: authenticator = ZAPAuthenticator(context) authenticator.deny('127.0.0.1') context.set_zap_authenticator(authenticator) req_socket = context.socket(azmq.REQ) rep_socket = context.socket(azmq.REP) try: req_socket.connect(ENDPOINT) rep_socket.bind(ENDPOINT) await req_socket.send_multipart([b'my', b'request']) with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(rep_socket.recv_multipart(), 0.25) finally: req_socket.close() rep_socket.close()
def test_plain_unknown_username(event_loop): async with azmq.Context() as context: authenticator = ZAPAuthenticator(context) authenticator.add_user(username='user', password='pwd') context.set_zap_authenticator(authenticator) req_socket = context.socket( azmq.REQ, mechanism=PlainClient(username='user2', password='pwd'), ) rep_socket = context.socket(azmq.REP, mechanism=PlainServer()) try: req_socket.connect(ENDPOINT) rep_socket.bind(ENDPOINT) await req_socket.send_multipart([b'my', b'request']) with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(rep_socket.recv_multipart(), 0.25) finally: req_socket.close() rep_socket.close()
def test_plain_invalid_password(event_loop): async with azmq.Context() as context: authenticator = ZAPAuthenticator(context) authenticator.add_user(username='user', password='pwd') context.set_zap_authenticator(authenticator) req_socket = context.socket( azmq.REQ, mechanism=PlainClient(username='user', password='pwd2'), ) rep_socket = context.socket(azmq.REP, mechanism=PlainServer()) try: req_socket.connect(ENDPOINT) rep_socket.bind(ENDPOINT) await req_socket.send_multipart([b'my', b'request']) with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(rep_socket.recv_multipart(), 0.25) finally: req_socket.close() rep_socket.close()
def test_timeout(loop): canceled_raised = False @asyncio.coroutine def long_running_task(): try: yield from asyncio.sleep(10, loop=loop) except asyncio.CancelledError: nonlocal canceled_raised canceled_raised = True raise with pytest.raises(asyncio.TimeoutError): with timeout(0.01, loop=loop) as t: yield from long_running_task() assert t._loop is loop assert canceled_raised, 'CancelledError was not raised'