---
title: StreamingQuery
description: "StreamingQuery class in PySpark: A handle to a query that is executing continuously in the background as new data arrives. All methods are thread-safe."
canonical_url: https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery
---

# StreamingQuery

A handle to a query that is executing continuously in the background as new data arrives. All methods are thread-safe.

## Syntax

```python
# Returned by DataStreamWriter.start() or DataStreamWriter.toTable()
q = df.writeStream.format("console").start()
```

## Properties

| Property                                                                                                          | Description                                                                                       |
| ----------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [`id`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/id.md)                         | Returns the unique ID of this query that persists across restarts from checkpoint data.           |
| [`runId`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/runId.md)                   | Returns the unique ID of this query that does not persist across restarts.                        |
| [`name`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/name.md)                     | Returns the user-specified name of the query, or `None` if not specified.                         |
| [`isActive`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/isActive.md)             | Returns whether this streaming query is currently active.                                         |
| [`status`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/status.md)                 | Returns the current status of the query as a dict.                                                |
| [`recentProgress`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/recentProgress.md) | Returns an array of the most recent `StreamingQueryProgress` updates for this query.              |
| [`lastProgress`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/lastProgress.md)     | Returns the most recent `StreamingQueryProgress` update, or `None` if there have been no updates. |

## Methods

| Method                                                                                                                         | Description                                                                                                       |
| ------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------- |
| [`awaitTermination(timeout)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/awaitTermination.md) | Waits for the termination of this query, either by `stop()` or by an exception.                                   |
| [`processAllAvailable()`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/processAllAvailable.md)  | Blocks until all available data in the source has been processed and committed to the sink. Intended for testing. |
| [`stop()`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/stop.md)                                | Stops this streaming query.                                                                                       |
| [`explain(extended)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/explain.md)                  | Prints the (logical and physical) plans to the console for debugging.                                             |
| [`exception()`](https://docs.databricks.com/aws/en/pyspark/reference/classes/streamingquery/exception.md)                      | Returns the `StreamingQueryException` if the query terminated with an exception, or `None`.                       |

## Examples

```python
sdf = spark.readStream.format("rate").load()
sq = sdf.writeStream.format('memory').queryName('this_query').start()
sq.isActive
# True
sq.name
# 'this_query'
sq.awaitTermination(5)
# False
sq.stop()
sq.isActive
# False
```
