我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用asyncio.AbstractEventLoop()。
def send_email(loop: asyncio.AbstractEventLoop, mail_from: str, mail_to: Union[Iterable, str], subject: str, body: str, server: str='localhost') -> None: """Send an email to one or more recipients. Only supports plain text emails with a single message body. No attachments etc. """ if type(mail_to) == str: mail_to = [mail_to] smtp = aiosmtplib.SMTP(hostname=server, port=25, loop=loop) try: await smtp.connect() for rcpt in mail_to: msg = MIMEText(body) msg['Subject'] = subject msg['From'] = mail_from msg['To'] = rcpt await smtp.send_message(msg) await smtp.quit() except aiosmtplib.errors.SMTPException as e: log.msg('Error sending smtp notification: %s' % (str(e)), 'NOTIFICATIONS')
def initialize(loop: asyncio.AbstractEventLoop, port: int, username: str, password: str, dbcon: DBConnection, active_monitor_manager: ActiveMonitorManager) -> None: """Initialize the webapi listener.""" stats.set('num_calls', 0, 'WEBAPI') app = web.Application(loop=loop, logger=log.logger, middlewares=[ middleware.logging_middleware_factory, middleware.error_handler_middleware_factory, middleware.basic_auth_middleware_factory, ]) app['username'] = username app['password'] = password app['dbcon'] = dbcon app['active_monitor_manager'] = active_monitor_manager setup_routes(app) listener = loop.create_server(app.make_handler(), '0.0.0.0', port) loop.create_task(listener) log.msg('Webapi listening on port %s' % port)
def test_get_child_watcher_thread(self): def f(): policy.set_event_loop(policy.new_event_loop()) self.assertIsInstance(policy.get_event_loop(), asyncio.AbstractEventLoop) watcher = policy.get_child_watcher() self.assertIsInstance(watcher, asyncio.SafeChildWatcher) self.assertIsNone(watcher._loop) policy.get_event_loop().close() policy = self.create_policy() th = threading.Thread(target=f) th.start() th.join()
def __init__(self, loop: asyncio.AbstractEventLoop, host: str, port: int, options: ClientOptions): self.host = host self.port = port self.loop = loop self.reader = None self.writer = None self.read_loop_task = None self.is_mongos = False self.is_writable = False self.max_bson_size = common.MAX_BSON_SIZE self.max_message_size = common.MAX_MESSAGE_SIZE self.max_wire_version = 0 self.max_write_batch_size = common.MAX_WRITE_BATCH_SIZE self.options = options self.slave_ok = False self.__connected = asyncio.Event(loop=loop) self.__disconnected = asyncio.Event(loop=loop) self.__request_id = 0 self.__request_futures = {} self.__sleeper = IncrementalSleeper(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=loop, future_store=future_store.get_child(), connection=connection, channel_number=channel_number, publisher_confirms=publisher_confirms, on_return_raises=on_return_raises, ) self._closed = False self._exchanges = dict() self._queues = dict() self._qos = 0, 0
def __init__(self, channel: Channel, publish_method, name: str, type: ExchangeType = ExchangeType.DIRECT, *, auto_delete: Optional[bool], durable: Optional[bool], internal: Optional[bool], passive: Optional[bool], arguments: dict = None, loop: asyncio.AbstractEventLoop, future_store: FutureStore): super().__init__( channel=channel, publish_method=publish_method, name=name, type=type, auto_delete=auto_delete, durable=durable, internal=internal, passive=passive, arguments=arguments, loop=loop, future_store=future_store ) self._bindings = dict()
def exception_handler(loop: asyncio.AbstractEventLoop, context: Dict[str, Any]) -> None: f = io.StringIO() print("Unhandled Exception", file=f) print("Message: ", context['message'], file=f) print("-- Context --", file=f) pprint(context, stream=f) print("-- Stack --", file=f) task = context.get('task', context.get('future')) if hasattr(task, 'print_stack'): task.print_stack(file=f) else: print("Cannot print stack", file=f) logger = get_logger("main", "exception_handler") logger.error(f.getvalue())
def test_task_yield_from_invalid(self): def bar(): pass if hasattr(asyncio.AbstractEventLoop, 'create_task'): err_msg = (r"^greenlet.yield_from was supposed to receive " r"only Futures, got .* in task .*$") else: err_msg = (r'^"greenio\.yield_from" was supposed to be called ' r'from a "greenio\.task" or a subsequent coroutine$') @asyncio.coroutine def foo(): with self.assertRaisesRegex(RuntimeError, err_msg): greenio.yield_from(bar) self.loop.run_until_complete(foo())
def __init__( self, url: str, bytes_range: Tuple[int, int], buffer_file_path, *, loop: AbstractEventLoop = None, server_logger=server_logger, chunk_size=DEFAULT_CHUNK_SIZE, chunk_download_timeout=DEFAULT_CHUNK_DOWNLOAD_TIMEOUT): self._url = url self._bytes_range = bytes_range self._length = bytes_range[1] - bytes_range[0] + 1 self._buffer_file_path = buffer_file_path self._loop = loop if loop is not None else asyncio.get_event_loop() self._server_logger = server_logger self._chunk_size = chunk_size self._chunk_download_timeout = chunk_download_timeout self._headers = {'Range': 'bytes={0[0]!s}-{0[1]!s}'.format(self._bytes_range)} self._bytes_downloaded = 0 self._state = NOT_STARTED
def __init__(self, vault_url: str = VAUTL_URL, token: Optional[str] = None, verify: bool = True, timeout: int = 10, session: Optional[aiohttp.ClientSession]=None, loop: asyncio.AbstractEventLoop=None): self.loop = loop if loop is None: self.loop = asyncio.get_event_loop() self.vault_url = vault_url.rstrip('/') self.session = session if session is None: if not verify: connector = aiohttp.TCPConnector(verify_ssl=False, loop=self.loop) else: connector = None self.session = aiohttp.ClientSession(connector=connector, read_timeout=timeout, conn_timeout=timeout, loop=self.loop) # pylint: disable=unexpected-keyword-arg self._auth_token = token self.timeout = timeout
def __init__( self, REMOTE_IP, REMOTE_PORT, loop: asyncio.AbstractEventLoop() = None, executor: futures.Executor() = None ): self._input_list = [] self._input_queue = asyncio.Queue() if loop: self.loop = loop else: self.loop = asyncio.get_event_loop() self.executor = executor self.REMOTE_IP = REMOTE_IP self.REMOTE_PORT = REMOTE_PORT
def __init__( self, device_list=[], connection_list=[], loop: asyncio.AbstractEventLoop() = None, executor: futures.Executor() = None ): if loop: self.loop = loop else: self.loop = asyncio.get_event_loop() if executor: self.executor = executor else: self.executor = futures.ProcessPoolExecutor() self.loop.set_default_executor(self.executor) self.device_list = device_list self.connection_list = connection_list
def __init__(self, server_url: str=None, enable_async: bool=False, schema: dict=None, request_kwargs: dict=None, loop: 'AbstractEventLoop'=None) -> None: self._server: ParseResult self.enable_async = enable_async self._request_kwargs: dict = request_kwargs or {} if server_url: self._server = urlparse(server_url) else: self._server = None self.resources_by_resource_identifier: \ 'Dict[Tuple[str, str], ResourceObject]' = {} self.resources_by_link: 'Dict[str, ResourceObject]' = {} self.documents_by_link: 'Dict[str, Document]' = {} self.schema: Schema = Schema(schema) if enable_async: import aiohttp self._aiohttp_session = aiohttp.ClientSession(loop=loop)
def __init__(self, io_loop: asyncio.AbstractEventLoop = None): super().__init__() self.io_loop = io_loop or asyncio.get_event_loop() self.sub_client = self.io_loop.run_until_complete( aioredis.create_redis((config.get('REDIS', 'host', fallback='localhost'), config.getint('REDIS', 'port', fallback=6379)), db=config.getint('REDIS', 'db', fallback=1))) self.redis_client = redis.StrictRedis( host=config.get('REDIS', 'host', fallback='localhost'), db=config.getint('REDIS', 'db', fallback=1), decode_responses=True) self.initialized = False self.sub_tasks = list() self.sub_channels = list() self.channel_router = dict() self.crontab_router = defaultdict(dict) self.datetime = None self.time = None self.loop_time = None
def create_connection(service, address=('127.0.0.1', 6000), *, protocol_cls=TBinaryProtocol, timeout=None, loop=None, **kw): """Create a thrift connection. This function is a :ref:`coroutine <coroutine>`. Open a connection to the thrift server by address argument. :param service: a thrift service object :param address: a (host, port) tuple :param protocol_cls: protocol type, default is :class:`TBinaryProtocol` :param timeout: if specified, would raise `asyncio.TimeoutError` if one rpc call is longer than `timeout` :param loop: :class:`Eventloop <asyncio.AbstractEventLoop>` instance, if not specified, default loop is used. :param kw: params relaied to asyncio.open_connection() :return: newly created :class:`ThriftConnection` instance. """ host, port = address reader, writer = yield from asyncio.open_connection( host, port, loop=loop, **kw) iprotocol = protocol_cls(reader) oprotocol = protocol_cls(writer) return ThriftConnection(service, iprot=iprotocol, oprot=oprotocol, address=address, loop=loop, timeout=timeout)
def create_pool(service, address=('127.0.0.1', 6000), *, minsize=1, maxsize=10, loop=None, timeout=None): """ Create a thrift connection pool. This function is a :ref:`coroutine <coroutine>`. :param service: service object defined by thrift file :param address: (host, port) tuple, default is ('127.0.0.1', 6000) :param minsize: minimal thrift connection, default is 1 :param maxsize: maximal thrift connection, default is 10 :param loop: targeting :class:`eventloop <asyncio.AbstractEventLoop>` :param timeout: default timeout for each connection, default is None :return: :class:`ThriftPool` instance """ pool = ThriftPool(service, address, minsize=minsize, maxsize=maxsize, loop=loop, timeout=timeout) try: yield from pool.fill_free(override_min=False) except Exception: pool.close() raise return pool
def _tearDown(self): ''' Destroy the event loop ''' if asyncio.iscoroutinefunction(self.tearDown): self.loop.run_until_complete(self.tearDown()) else: self.tearDown() if not isinstance(self.loop, asyncio.AbstractEventLoop): raise Exception('Invalid event loop: ', self.loop) if self.loop.is_running(): self.loop.stop() self.loop.close() del self.loop asyncio.set_event_loop_policy(None) asyncio.set_event_loop(None) # By explicitly forcing a garbage collection here, # the event loop will report any remaining sockets # and coroutines left in the event loop which indicates # that further cleanup actions should be implemented # in the code under test. gc.collect()
def __init__(self, api_key: str = None, auto_rate_limit: bool = True, event_loop: asyncio.AbstractEventLoop = None, _api_version: int = 1): if api_key is None: raise custom_exceptions.NoAPIKeyError("No api key was supplied to client initialization.") else: self._api_key = api_key self.auto_ratelimit = auto_rate_limit if event_loop is None: self._event_loop = asyncio.get_event_loop() else: self._event_loop = event_loop self._api_version = _api_version
def invoke(self, rpc: 'venom.stub.RPC', request: 'venom.message.Message', *, context: 'venom.rpc.RequestContext' = None, loop: asyncio.AbstractEventLoop = None, timeout: int = None): if loop is None: loop = asyncio.get_event_loop() future = loop.run_in_executor(None, partial(self._grpc_stub.blocking_unary_unary, self._group, rpc.name, request, timeout=timeout)) return await future
def enable(statsd_client: 'statsd.StatsClient', interval: float = 0.25, loop: asyncio.AbstractEventLoop = None) -> None: ''' Start logging event loop lags to StatsD. In ideal circumstances they should be very close to zero. Lags may increase if event loop callbacks block for too long. ''' if loop is None: loop = asyncio.get_event_loop() async def monitor(): while loop.is_running(): t0 = loop.time() await asyncio.sleep(interval) lag = loop.time() - t0 - interval # Should be close to zero. statsd_client.timing('aiodebug.monitor_loop_lag', lag * 1000) loop.create_task(monitor())
def enable(stack_output_dir: str, interval: float = 0.25, loop: asyncio.AbstractEventLoop = None) -> Tuple[TraceDumper, asyncio.Task]: ''' Start detecting hangs in asyncio loop. If a hang for more than `interval` is detected, a stack trace is saved into `stack_output_dir`. ''' if loop is None: loop = asyncio.get_event_loop() # This value is wrapped into a list, so python doesnt pass the number by value but by reference. last_loop_iteration_time_wrapped = [time.monotonic()] tracer = TraceDumper(stack_output_dir, interval, last_loop_iteration_time_wrapped) tracer.setDaemon(True) async def monitor(): while loop.is_running(): last_loop_iteration_time_wrapped[0] = time.monotonic() await asyncio.sleep(interval / 2.) monitor_task = loop.create_task(monitor()) tracer.start() return tracer, monitor_task
def __init__(self, logger: utils.NTLogger=None, limit: int=8, session: aiohttp.ClientSession=None, return_session: bool=False, loop: asyncio.AbstractEventLoop=None, ): """ ????????????????? :param T<= logging.logger logger: ??? :param int limit: ???????????? :param aiohttp.ClientSession session: :param bool return_session: ????????????? :param asyncio.AbstractEventLoop loop: ??????? """ super().__init__(loop=loop, logger=logger) self.undone = [] self.__bucket = {} self.session = session or self.loop.run_until_complete(self.get_session()) self.__return_session = return_session self.__parallel_limit = limit
def start_worker(worker_path: str, worker_class: str, burst: bool, loop: asyncio.AbstractEventLoop=None): """ Run from within the subprocess to load the worker class and execute jobs. :param worker_path: full path to the python file containing the worker definition :param worker_class: name of the worker class to be loaded and used :param burst: whether or not to run in burst mode :param loop: asyncio loop use to or None """ worker_cls = import_string(worker_path, worker_class) work_logger.info('Starting "%s" on pid=%d', worker_cls.__name__, os.getpid()) worker = worker_cls(burst=burst, loop=loop) try: worker.run_until_complete(log_redis_version=True) except BaseException as e: work_logger.exception('Worker exiting after an unhandled error: %s', e.__class__.__name__) # could raise here instead of sys.exit but that causes the traceback to be printed twice, # if it's needed "raise_exc" would need to be added a new argument to the function sys.exit(1) finally: worker.loop.run_until_complete(worker.close())
def get_or_create_event_loop() -> asyncio.AbstractEventLoop: """ Get or create the current event loop. Gets the current event loop if it exists, else creates, sets as global, and then returns an asyncio event loop. Args: None Returns: event_loop - an asyncio event loop """ try: loop = asyncio.get_event_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) return loop
def __init__( self, token: str, *, base_url: str=_DEFAULT_BASE_URL, loop: Optional[asyncio.AbstractEventLoop]=None) -> None: self._loop = loop or asyncio.get_event_loop() self._token = token self._base_url = base_url self._client = aiohttp.ClientSession(loop=self._loop) self._err_times = 0 self._pending_updates = [] # type: List[dikuto.BotoDikuto] self._update_offset = 0
def __init__(self, config: Any, *, loop: asyncio.AbstractEventLoop=None) -> None: self.loop = loop or asyncio.get_event_loop() if not config: log.msg('Missing config section, no alert notification will be sent', 'NOTIFICATIONS') self.http_settings = None self.email_settings = None self.sms_settings = None self.slack_settings = None else: self.http_settings = http.parse_settings(config) self.email_settings = email.parse_settings(config) self.sms_settings = sms.parse_settings(config) self.slack_settings = slack.parse_settings(config)
def send_alert_notification( loop: asyncio.AbstractEventLoop, settings: Dict[str, Any], recipients: Iterable[str], tmpl_args: Dict[str, Any]) -> None: subject = settings['tmpl-subject'].render(**tmpl_args) body = settings['tmpl-body'].render(**tmpl_args) await send_email(loop, settings['sender'], recipients, subject, body, settings['server'])
def mainloop(loop: asyncio.AbstractEventLoop, config: configparser.ConfigParser, dbcon: sql.DBConnection, active_monitor_manager: ActiveMonitorManager): """Perform all setup that requires the event loop and then just wait around forever.""" await dbcon.initialize() await active_monitor_manager.initialize() webapi.initialize( loop, int(config.get('WEBAPI', 'port', fallback='10000')), config.get('WEBAPI', 'username'), config.get('WEBAPI', 'password'), dbcon, active_monitor_manager, ) if config.has_section('WEBMGMT'): from irisett.webmgmt import webmgmt webmgmt.initialize( loop, int(config.get('WEBMGMT', 'port', fallback='11000')), config.get('WEBMGMT', 'username'), config.get('WEBMGMT', 'password'), dbcon, active_monitor_manager, ) active_monitor_manager.start() stats.set('global_startup', time.time()) log.msg('Irisett startup complete') while True: await asyncio.sleep(10) # noinspection PyUnresolvedReferences
def __init__(self, host: str, user: str, passwd: str, dbname: str, loop: asyncio.AbstractEventLoop=None) -> None: self.loop = loop or asyncio.get_event_loop() self.host = host self.user = user self.passwd = passwd self.dbname = dbname self.pool = None # type: Any stats.set('queries', 0, 'SQL') stats.set('transactions', 0, 'SQL')
def initialize(loop: asyncio.AbstractEventLoop, port: int, username: str, password: str, dbcon: DBConnection, active_monitor_manager: ActiveMonitorManager) -> None: """Initialize the webmgmt listener.""" stats.set('num_calls', 0, 'WEBMGMT') app = web.Application(loop=loop, logger=log.logger, middlewares=[ middleware.logging_middleware_factory, middleware.error_handler_middleware_factory, middleware.basic_auth_middleware_factory, ]) app['username'] = username app['password'] = password app['dbcon'] = dbcon app['active_monitor_manager'] = active_monitor_manager setup_routes(app) aiohttp_jinja2.setup( app, loader=jinja2.PackageLoader('irisett.webmgmt', 'templates'), filters={ 'timestamp': jinja_filters.timestamp }, ) listener = loop.create_server(app.make_handler(), '0.0.0.0', port) asyncio.ensure_future(listener) log.msg('Webmgmt listening on port %s' % port)
def __init__(self, bot_configuration: BotConfiguration, session: aiohttp.ClientSession, loop: aio.AbstractEventLoop): self._logger = logging.getLogger('aioviber.api') self._bot_configuration = bot_configuration self._viber_bot_api_url = VIBER_BOT_API_URL self.session = session self.loop = loop
def test_get_event_loop(self): policy = asyncio.DefaultEventLoopPolicy() self.assertIsNone(policy._local._loop) loop = policy.get_event_loop() self.assertIsInstance(loop, asyncio.AbstractEventLoop) self.assertIs(policy._local._loop, loop) self.assertIs(loop, policy.get_event_loop()) loop.close()
def test_new_event_loop(self): policy = asyncio.DefaultEventLoopPolicy() loop = policy.new_event_loop() self.assertIsInstance(loop, asyncio.AbstractEventLoop) loop.close()
def __init__(self, uri: str, loop: asyncio.AbstractEventLoop): uri_info = parse_uri(uri=uri) assert len(uri_info['nodelist']) == 1, 'Can only connect to single node - either mongod or mongos' self.host = uri_info['nodelist'][0][0] self.port = uri_info['nodelist'][0][1] self.options = ClientOptions( uri_info['username'], uri_info['password'], uri_info['database'], uri_info['options'] ) self.loop = loop self._pool = [] self.__default_database_name = uri_info['database']
def create(cls, loop: asyncio.AbstractEventLoop, host: str, port: int, options: ClientOptions) -> 'Connection': conn = cls(loop, host, port, options) await conn.connect() return conn
def __init__(self, loop: asyncio.AbstractEventLoop, max_delay: float = 10.0): self.delay = self.initial_delay self.loop = loop self.max_delay = max_delay
def create_client(uri: str, loop: Optional[asyncio.AbstractEventLoop] = None) -> AioMongoClient: if loop is None: loop = asyncio.get_event_loop() cl = AioMongoClient(uri, loop) await cl.connect() return cl
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 loop(self) -> asyncio.AbstractEventLoop: return self._amqp_queue.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, loop: asyncio.AbstractEventLoop, future_store: FutureStore, channel: Channel, name, durable, exclusive, auto_delete, arguments): super().__init__(loop, future_store, channel, name or "amq_%s" % shortuuid.uuid(), durable, exclusive, auto_delete, arguments) self._consumers = {} self._bindings = {}
def __init__(self, loop: asyncio.AbstractEventLoop): self.loop = loop self.handlers = {} self.readers = set() self.writers = set()
def __init__(self, channel: Channel): """ Creates a new :class:`Master` instance. :param channel: Initialized instance of :class:`aio_pika.Channel` """ self.channel = channel # type: Channel self.loop = self.channel.loop # type: asyncio.AbstractEventLoop self.proxy = Proxy(self.create_task) self.channel.add_on_return_callback(self.on_message_returned)
def __init__(self, loop: asyncio.AbstractEventLoop, main_store: 'FutureStore'=None): self.__main_store = main_store self.__collection = set() self.__loop = loop or asyncio.get_event_loop()
def __init__(self, loop: asyncio.AbstractEventLoop, future_store: FutureStore): self.loop = loop self._futures = future_store self._closing = create_future(loop=self.loop)
def ping(self, loop: asyncio.AbstractEventLoop=asyncio.get_event_loop()): await self.apps.list(loop=loop)
def __init__(self, host: str='0.0.0.0', port: int=10001, loop: asyncio.AbstractEventLoop=asyncio.get_event_loop(), logger=None, debug=False): v1_service = service.VersionedService( [ apps.AppV1Controller, routes.AppRouteV1Controller, runnable.RunnableV1Controller, ], middleware=[ keystone.auth_through_token, content_type.content_type_validator ]) public_runnable_service = service.VersionedService( [ runnable.PublicRunnableV1Controller ], middleware=[ content_type.content_type_validator, ] ) super(API, self).__init__( host=host, port=port, event_loop=loop, logger=logger, debug=debug, subservice_definitions=[ v1_service, public_runnable_service ] )
def _loop(self) -> asyncio.AbstractEventLoop: assert isinstance(self._ctx, context.AsyncioSketchContext) return self._ctx.loop