pyspark.sql.streaming.StreamingQueryManager.awaitAnyTermination

StreamingQueryManager.awaitAnyTermination(timeout: Optional[int] = None) → Optional[bool][source]

Wait until any of the queries on the associated SparkSession has terminated since the creation of the context, or since resetTerminated() was called. If any query was terminated with an exception, then the exception will be thrown. If timeout is set, it returns whether the query has terminated or not within the timeout seconds.

If a query has terminated, then subsequent calls to awaitAnyTermination() will either return immediately (if the query was terminated by query.stop()), or throw the exception immediately (if the query was terminated with exception). Use resetTerminated() to clear past terminations and wait for new terminations.

In the case where multiple queries have terminated since resetTermination() was called, if any query has terminated with exception, then awaitAnyTermination() will throw any of the exception. For correctly documenting exceptions across multiple queries, users need to stop all of them after any of them terminates with exception, and then check the query.exception() for each query.

throws StreamingQueryException, if this query has terminated with an exception

New in version 2.0.0.

Changed in version 3.5.0: Supports Spark Connect.

Parameters
timeoutint, optional

default None. The waiting time for any streaming query to terminate.

Returns
bool, optional

The result whether any streaming query has terminated or not within the timeout seconds if timeout is set. The StreamingQueryException will be thrown if any query has terminated with an exception.

Examples

>>> sdf = spark.readStream.format("rate").load()
>>> sq = sdf.writeStream.format('memory').queryName('this_query').start()

Return whether any of the query on the associated SparkSession has terminated or not within 5 seconds

>>> spark.streams.awaitAnyTermination(5)
True
>>> sq.stop()