---
title: KeepAwake
description: A running keepalive. Use as a context manager, or call stop().
---

```python
@final
class KeepAwake
```

A running keepalive. Use as a context manager, or call `stop()`.

Keep a reference: dropping this object stops the keepalive.

## Constructor

`KeepAwake` declares no `__new__` or `__init__`, so it is not constructed directly: an instance comes back from another call in this package.

## Properties

### `running`

```python
running: bool
```

Whether the keepalive is still polling.

## Methods

### `stop`

```python
def stop() -> KeepAwakeReport
```

Stops polling and returns the report. Raises the poll's error if one ended it.

### `stop_async`

```python
def stop_async() -> KeepAwakeReport
```

The awaitable twin of `stop`. The stop is asked for before the first await, so
cancelling this still stops the keepalive; only the wait for its report is given up.

### `wait`

```python
def wait(timeout: SupportsFloat | SupportsIndex | None = None) -> KeepAwakeReport | None
```

Waits for the keepalive to end on its own (`while_busy` or `max_duration`).

Returns `None` if it is still running when `timeout` seconds pass.

### `wait_async`

```python
def wait_async(
    timeout: SupportsFloat | SupportsIndex | None = None,
) -> KeepAwakeReport | None
```

The awaitable twin of `wait`.

## Special methods

### `__aenter__`

```python
def __aenter__() -> KeepAwake
```

`async with session.keep_awake() as k:`.

### `__aexit__`

```python
def __aexit__(
    exc_type: Any | None = None,
    exc_value: Any | None = None,
    traceback: Any | None = None,
) -> bool
```

`__exit__`, awaited.

### `__enter__`

```python
def __enter__() -> KeepAwake
```

### `__exit__`

```python
def __exit__(
    exc_type: Any | None = None,
    exc_value: Any | None = None,
    traceback: Any | None = None,
) -> bool
```

Stops the keepalive. A poll error does not mask an exception already in flight.

### `__repr__`

```python
def __repr__() -> str
```

## Provenance

This page is generated from `bindings/microvms-py/microvms.pyi` by Griffe 2.3.0, run from `site/scripts/gen-reference.mjs` on every `pnpm run sync`, so an edit made here is overwritten by the next run.

To change the page, change the doc comment in the binding's Rust source, then regenerate the declarations with `mise run stubs`. `mise run stubs:check` fails when the committed file no longer matches the source.