예제 #1
0
async def cancel(task: asyncio.Task):
    if not (task.done() or task.cancelled()):
        try:
            task.cancel()
            await task
        except asyncio.CancelledError:
            pass
예제 #2
0
async def finalize_task(task: Task) -> Any:
    try:
        return await task

    finally:
        if not task.cancelled():
            task.cancel()
예제 #3
0
 async def _cancel_task_if_exists(task: asyncio.Task):
     if task:
         task.cancel()
         try:
             await task
         except asyncio.CancelledError:
             pass
예제 #4
0
    def _send_then_recv(self, send, recv):
        fut_recv = Task(recv())
        result = None
        for i in range(self.max_tries):
            try:
                yield from send()
            except ConnectionError as e:
                logging.warn("Failed to send RADIUS request: %s" % e)
                yield from sleep(TIMEOUT, loop=self.loop)
                continue

            try:
                result = yield from wait_for(shield(fut_recv), self.timeout)
                break
            except TimeoutError:
                # No need to restart task, since it is protected by shield().
                logging.warning("Timeout, re-send RADIUS request.")
            except ValueError as e:
                logging.warning("Malformed RADIUS packet received: %s" % e)
                logging.info("Please check the shared secret.")
                fut_recv = Task(self._recv_response())
            except ConnectionError as e:
                logging.warn("Failed to receive RADIUS response: %s" % e)
                yield from sleep(TIMEOUT, loop=self.loop)
                fut_recv = Task(self._recv_response())

        if result is None:
            logging.warning("Timeout. No valid RADIUS response.")
            fut_recv.cancel()
        return result
예제 #5
0
    async def _stop(self, kill_task: asyncio.Task, grace_time: int) -> None:
        """Stop docker container task

        Args:
            kill_task: kill task as asyncio future
            grace_time: timout before sigkill
        """
        time_start = time.time()
        # I have no idea why this happens but without a sleep the event loop
        # gets blocked and the container takes an extra 10 seconds to stop
        await asyncio.sleep(1)
        LOGGER.info(f"stopping docker container {self._name}")
        # for better logging we are handling our own sigkill
        # so add 1 to timout so will not be called
        try:
            self._process.stop(timeout=grace_time + 1)
        except docker.errors.NotFound:
            pass
        LOGGER.debug(
            f"container {self._name} stopped in {int(time.time() - time_start)} seconds",
            False)

        kill_task.cancel()

        # fix for gitlab ci failing to stop containers
        if self._unique_name in [
                cont.name for cont in client.containers.list(all=True)
        ]:
            await self._kill(0)
async def cancel_await(task: Task,
                       callback: Optional[Callable[[], None]] = None) -> None:
    task.cancel()
    try:
        await task
    except asyncio.CancelledError:
        if callback is not None:
            callback()
예제 #7
0
        def on_timeout(task: asyncio.Task, loop: asyncio.AbstractEventLoop):
            nonlocal cancelled

            if task.done():
                return

            task.cancel()
            cancelled = True
async def ensureTaskCanceled(asyncio_task: asyncio.Task) -> None:
    while not asyncio_task.done():
        try:
            asyncio_task.cancel()
        except:
            pass
        finally:
            await asyncio.sleep(0)
예제 #9
0
def delete_delay(channel):
    if channel not in chat_groups:
        return

    c = chat_groups[channel]
    if 'delete' in c:
        Task.cancel(c['delete'])
    task = asyncio.create_task(delete_coroutine(channel))
    c["delete"] = task
예제 #10
0
async def _cancel_task_if_client_disconnected(
    request: Request, task: asyncio.Task, interval: float = _DEFAULT_CHECK_INTERVAL_S
) -> None:
    with suppress(CancelledError):
        while True:
            if await request.is_disconnected():
                logger.warning("client %s disconnected!", request.client)
                task.cancel()
                break
            await asyncio.sleep(interval)
예제 #11
0
 def _task_finished(self, task: Task) -> None:
     # If a task raised an exception (other than CancelledError), cancel all other tasks
     if not task.cancelled() and task.exception() is not None:
         if self._status is NurseryStatus.running:
             self._status = NurseryStatus.closed
             self._host_task.cancel()
             for task in self._tasks:
                 task.cancel()
     else:
         self._tasks.discard(task)
예제 #12
0
async def get_response_item_count(task: Task, done: Set[Awaitable],
                                  pending: Set[Awaitable],
                                  error_msg: str) -> Optional[int]:
    if task in done and task.exception() is None:
        return len(await task.result().json())
    elif task in pending:
        task.cancel()
    else:
        logging.exception(error_msg, exc_info=task.exception())

    return None
예제 #13
0
파일: cmd.py 프로젝트: exec-all/numad
def display_loop(term, es_servers):
	"""Starts and waits on inputs, slightly mangles data and sends them to be rendered
	
	This corresponds loosly with an event loop in game programming
	"""
	query = ""
	# avoid recreating this all the time due to implicit dns lookups in creation
	es = Elasticsearch(es_servers)

	loop = asyncio.get_event_loop()

	input = Task(input_loop(loop, term, query))
	search = Task(search_loop(loop, es, query))

	jobs = [input, search]
	
	while True:
		done, pending = yield from asyncio.wait(jobs)
		jobs = []


		# this needs to come before input handling as it may try
		# and cancel a compleated job before its processed. as resuls
		# take a bit to come back its better to just briefly display them
		if search in done:
			results = search.result()
			render_search_results(term, results)

		if input in done:
			query = input.result()
			render_query_field(term, query)
			# we wrap this is a task for the membership query above
			# if we just pass in a corutine, asyncio.wait takes it upon
			# itself to wrap that in a Task and the comparison will always
			# fail
			input = Task(input_loop(loop, term, query))
			jobs.append(input)

			# ES does not return results to us for short queries, so just short
			# circuit them and avoid network traffic
			if len(query) > 1:
				# we wont be using the results anymore so just discard the worker
				search.cancel()
				search = Task(search_loop(loop, es, query))
				jobs.append(search)
			else:
				# force a clear of the results
				render_search_results(term, [])

		sys.stdout.flush()

		# Make sure we resubmit any jobs that have not yet been compleated
		# otherwise we leak them and things go into limbo
		jobs += pending
예제 #14
0
파일: test_proxy.py 프로젝트: ddqof/proxy
async def send_then_recv(
        proxy_port: int,
        proxy_task: asyncio.Task,
        server_task: asyncio.Task
):
    await asyncio.sleep(0.01)  # time to complete setting up servers
    cl_reader, cl_writer = await asyncio.open_connection(LOCALHOST, proxy_port)
    cl_writer.write(b"some message")
    try:
        return await cl_reader.read(4096)
    finally:
        proxy_task.cancel()
        server_task.cancel()
예제 #15
0
파일: sprinkler.py 프로젝트: pade/sprinkler
async def exit_safe(signal_nb: int, cur_task: asyncio.Task) -> None:
    """ Safe exit: stop all tasks before exiting """
    logger = logging.getLogger("sprinkler")
    if signal_nb == 15:
        sig = "SIGTERM"
    elif signal_nb == 2:
        sig = "SIGINT"
    elif signal_nb == 255:
        sig = "KEYBOARD INTERRUPT"
    else:
        sig = "OTHER SIGNAL"
    logger.info(f"Terminated by signal ({sig})")
    cur_task.cancel()
예제 #16
0
async def cancel(task: Task, loop: Optional[AbstractEventLoop]=None) -> None:
    """Cancel a task and wait until it's done.

    **Note**: this function is a coroutine.

    Canceling a child task and returning without waiting for the child task to
    complete is a common cause of "event loop closed" ``RuntimeError``
    exceptions, especially during program shutdown.  Therefore, this becomes a
    common pattern:

    .. code-block:: python

        task.cancel()
        await asyncio.wait({task})

    However, if the parent task itself is also canceled, then the
    ``asyncio.wait()`` call will be interrupted and the child task will still
    not complete.  To solve this, we must also manage to trap the
    ``asyncio.CancelledError`` exception and call ``asyncio.wait({task})``
    again and properly re-raise the ``asyncio.CancelledError`` exception.  For
    example:


    .. code-block:: python

        task.cancel()
        try:
            await asyncio.wait({task})
        except asyncio.CancelledError:
            await asyncio.wait({task})
            raise

    This is not trivial and must be done so many times in a program that
    cancels tasks that it merits a replacement API for ``task.cancel()``.

    :param task: The ``asyncio.Task`` object to cancel.
    :param loop: The event loop to use for awaiting.  Defaults to the current
     event loop.

    .. versionadded:: 0.3

    """

    loop = loop or asyncio.get_event_loop()

    task.cancel()
    try:
        await asyncio.wait({task}, loop=loop)
    except asyncio.CancelledError:
        await asyncio.wait({task}, loop=loop)
        raise
예제 #17
0
def set_timeout(task: asyncio.Task, timeout: [float, int], loop: asyncio.AbstractEventLoop = None, timeout_cancel=True):
    assert isinstance(timeout, (float, int))
    if loop is None:
        loop = get_running_loop()
    now_time = loop.time()
    out_time = now_time + timeout
    if timeout_cancel:
        if timeout <= 0:
            task.cancel()
            return
        unset_timeout(task)
        handle = loop.call_at(out_time, task.cancel)
        setattr(task, _MODULE_TIMEOUT_HANDLE, handle)
    setattr(task, _MODULE_TIMEOUT, out_time)
예제 #18
0
    async def _proxy_connection(
            self, in_read: Task, out_read: Task, in_reader: StreamReader,
            out_writer: StreamWriter) -> Optional[asyncio.Task]:

        data: bytes = in_read.result()

        if not data:
            out_read.cancel()
            return

        out_writer.write(data)
        await out_writer.drain()

        return asyncio.create_task(in_reader.read(512))
예제 #19
0
 def _run_until_task_done(self, task: asyncio.Task, deadline=None):
     try:
         self._wait_timeout = False
         if deadline is not None:
             deadline_handle = self._loop.call_later(max(0, deadline - time.time()), self._set_wait_timeout)
         while not self._wait_timeout and not task.done():
             if len(self._wait_idle_list) == 0:
                 self._run_once()
             else:
                 self._run_until_idle(async_run=True)
     finally:
         if deadline is not None:
             deadline_handle.cancel()
         task.cancel()
예제 #20
0
        def on_timeout(task: asyncio.Task, loop: asyncio.AbstractEventLoop):
            nonlocal cancelled

            if task.done():
                return

            task.cancel()
            cancelled = True

            @asyncio.coroutine
            def waiter():
                yield from task

            loop.create_task(waiter())
예제 #21
0
async def stop_supervisor(supervisor_task: asyncio.Task, logger: Logger):
    if not supervisor_task.done():
        supervisor_task.cancel()

        done, _ = await asyncio.wait([supervisor_task])

    if not supervisor_task.done():
        logger.warning("unable to stop")
    else:
        if not supervisor_task.cancelled():
            exc = supervisor_task.exception()
            if exc is not None:
                logger.exception('exception thrown on stop: %s',
                                 exc,
                                 exc_info=exc)
예제 #22
0
async def teardown_task(tz: DstTzInfo, task: asyncio.Task) -> None:
    tlog(f"consumer-teardown_task() - starting ")

    if not config.market_close:
        tlog(
            "we're probably in market schedule by-pass mode, exiting consumer-teardown_task()"
        )
        return

    to_market_close: timedelta
    try:
        dt = datetime.today().astimezone(tz)
        to_market_close = (config.market_close - dt if config.market_close > dt
                           else timedelta(hours=24) +
                           (config.market_close - dt))
        tlog(
            f"consumer-teardown_task() - waiting for market close: {to_market_close}"
        )
    except Exception as e:
        tlog(
            f"consumer-teardown_task() - exception of type {type(e).__name__} with args {e.args}"
        )
        return

    try:
        await asyncio.sleep(to_market_close.total_seconds() + 60 * 5)

        tlog("consumer-teardown_task() starting")
        await end_time("market close")

        tlog("consumer-teardown_task(): requesting tasks to cancel")
        task.cancel()
        try:
            await task
        except asyncio.CancelledError:
            tlog("consumer-teardown_task(): tasks are cancelled now")

    except asyncio.CancelledError:
        tlog("consumer-teardown_task() cancelled during sleep")
    except KeyboardInterrupt:
        tlog("consumer-teardown_task() - Caught KeyboardInterrupt")
    except Exception as e:
        tlog(
            f"consumer-teardown_task() - exception of type {type(e).__name__} with args {e.args}"
        )
        # asyncio.get_running_loop().stop()
    finally:
        tlog("consumer-teardown_task() task done.")
예제 #23
0
파일: __init__.py 프로젝트: jhofker/core-1
async def disconnect_client(
    hass: HomeAssistant,
    entry: ConfigEntry,
    client: ZwaveClient,
    listen_task: asyncio.Task,
    platform_task: asyncio.Task,
) -> None:
    """Disconnect client."""
    listen_task.cancel()
    platform_task.cancel()

    await asyncio.gather(listen_task, platform_task)

    if client.connected:
        await client.disconnect()
        LOGGER.info("Disconnected from Zwave JS Server")
예제 #24
0
 async def async_cancel(task: asyncio.Task, alv: AsyncLazyValue) -> None:
     start = time()
     while alv._awaiting_tasks < 1:
         # Sleep until both tasks are awaiting on the future (one of them
         # is awaiting on async_func, so we only wait until the count
         # is 1). Also, we timeout if we wait more than 3 seconds
         now = time()
         if now - start > 3:
             raise RuntimeError(
                 "cannot cancel since the tasks are not waiting on the future"
             )
         await asyncio.sleep(0)
     self.log("cancelling")
     task.cancel()
     self.cancelled.set()
     self.log("cancelled")
예제 #25
0
async def monitor_cancel(redis: aioredis.Redis, current_job_id: str,
                         task: asyncio.Task) -> asyncio.Task:
    """
    Watch the cancel channel in redis for the current job id.

    If the current job id is found, cancel the task.

    :param redis: The redis connection.
    :param current_job_id: The id of the current job.
    :param task: The asyncio task in which the job is being executed.

    :return: The asyncio task object after it has been cancelled.
    """
    async for id_ in redis_channel(redis, VIRTOOL_JOBS_CANCEL_CHANNEL):
        if id_ == current_job_id:
            task.cancel()
            return task
예제 #26
0
async def _cancel_task_if_client_disconnected(
    request: Request, task: asyncio.Task, interval: float = _DEFAULT_CHECK_INTERVAL_S
) -> None:
    try:
        while True:
            if task.done():
                logger.debug("task %s is done", task)
                break
            if await request.is_disconnected():
                logger.warning("client %s disconnected!", request.client)
                task.cancel()
                break
            await asyncio.sleep(interval)
    except CancelledError:
        logger.debug("task was cancelled")
        raise
    finally:
        logger.debug("task completed")
예제 #27
0
async def cancel_and_stop_task(task: Task):
    if task.cancelled():
        logger.debug('Task already canceled')
        return

    task.cancel()

    try:
        await task

    except CancelledError:
        logger.debug('Task has been canceled')

    except Exception as err:
        logger.exception(f'Task finished with error ({err}):')

    else:
        logger.debug('Task finished successfully')
예제 #28
0
async def disconnect_client(
    opp: OpenPeerPower,
    entry: ConfigEntry,
    client: ZwaveClient,
    listen_task: asyncio.Task,
    platform_task: asyncio.Task,
) -> None:
    """Disconnect client."""
    listen_task.cancel()
    platform_task.cancel()
    platform_setup_tasks = (opp.data[DOMAIN].get(entry.entry_id, {}).get(
        DATA_PLATFORM_SETUP, {}).values())
    for task in platform_setup_tasks:
        task.cancel()

    await asyncio.gather(listen_task, platform_task, *platform_setup_tasks)

    if client.connected:
        await client.disconnect()
        LOGGER.info("Disconnected from Zwave JS Server")
예제 #29
0
파일: test_proxy.py 프로젝트: ddqof/proxy
async def send_until_limit(
        proxy_port: int,
        proxy_task: asyncio.Task,
        server_task: asyncio.Task
):
    await asyncio.sleep(0.01)  # time to complete setting up servers
    first_reader, first_writer = await asyncio.open_connection(
        LOCALHOST, proxy_port
    )
    first_writer.write(b"some message")
    await first_writer.drain()
    second_reader, second_writer = await asyncio.open_connection(
        LOCALHOST, proxy_port
    )
    second_writer.write(b"this message will be blocked")
    await second_writer.drain()
    try:
        return await second_reader.read(4096)
    finally:
        proxy_task.cancel()
        server_task.cancel()
예제 #30
0
    async def watchdog(self, scheduler: asyncio.Task) -> None:
        """detect anomalies and trigger clean exit

        watchdog was originally intended to clean up after either consumer
        or producer quit and trigger the end of the thread.

        Override as appropriate.
        """
        loop_counter = 0
        while True:
            await asyncio.sleep(1.5)
            log.debug("watchdog: awoke")
            if self.stopped:
                raise Exception("Watchdog terminating scheduler")
                scheduler.cancel()
                log.warning("watchdog: scheduler cancelling")
                await scheduler
                log.warning("watchdog: scheduler cancelled")
                raise Exception("snarly watchdog")
            loop_counter += 1
        log.info("watchdog: quitting")
예제 #31
0
def sig_term_handler(manager_task: asyncio.Task):
    """Gracefully terminate the running process."""
    logger.debug("SIG_INT received, shutting down.")
    manager_task.cancel()
예제 #32
0
 async def async_cancel(task: asyncio.Task,
                        alv: AsyncLazyValue) -> None:
     await asyncio.wait_for(self.coro_running.wait(), timeout=3)
     self.log("cancelling")
     task.cancel()
     self.log("cancelled")