Asynchronous processing with transformWithState is now generally available
Python row-based transformWithState now supports native asynchronous processing with asyncio on Databricks, improving stateful streaming throughput.
O que mudou
Asynchronous processing for the Python row-based transformWithState API is now generally available (GA). Built on asyncio, this feature executes state operations and user processing logic concurrently, reducing idle time and boosting streaming throughput.
Use cases
Imagine you maintain a stateful streaming pipeline that groups records by key, updates running aggregates, or enriches data via external REST endpoints. Synchronous processing blocks worker threads during state retrieval and network calls.
With asynchronous processing, you can:
- Run state updates across grouping keys concurrently without needing third-party async streaming libraries.
- Trigger parallel external HTTP requests via
asyncio.gatherwithin your stateful processing logic. - Batch inter-process communication automatically to maximize overall query throughput.
How to use
To switch from synchronous logic, implement AsyncStatefulProcessor instead of StatefulProcessor, define the processing methods using async def, and consume state read/write operations using await.
from pyspark.sql import Row
from pyspark.sql.streaming import AsyncStatefulProcessor, AsyncStatefulProcessorHandle
from pyspark.sql.types import StructType, StructField, LongType
value_schema = StructType([StructField("count", LongType(), True)])
class AsyncCountProcessor(AsyncStatefulProcessor):
async def init(self, handle: AsyncStatefulProcessorHandle) -> None:
self.count = handle.getValueState("count", value_schema)
async def handleInputRows(self, key, rows, timerValues):
total = (await self.count.get() or (0,))[0]
for _ in rows:
total += 1
await self.count.update((total,))
yield Row(action=key[0], count=total)
async def close(self) -> None:
pass
What happens in this code:
- State variable instantiation via
getValueStateinsideinitstays synchronous. - The handler method
handleInputRowsis declared as an async coroutine (async def). - Reading (
self.count.get()) and updating (self.count.update()) state values use theawaitkeyword.
To run the query, pass the processor to transformWithState as usual:
q = (
events.groupBy("action")
.transformWithState(
statefulProcessor=AsyncCountProcessor(),
outputStructType=output_schema,
outputMode="Update",
timeMode="None",
)
.writeStream.format("memory")
.queryName("async_counts")
.trigger(availableNow=True)
.start()
)
q.awaitTermination()
What happens in this code:
- The
transformWithStatequery syntax remains identical to synchronous queries. - Because synchronous and asynchronous processors share the same internal state structure, queries can switch implementations using the same checkpoint directory.
Limitations and requirements
- Requires Databricks Runtime 19 or higher, or serverless compute environment version 6 or higher.
- In Lakeflow pipelines, the pipeline channel must be set to
PREVIEW. - Supported only for the Python row-based
transformWithStateAPI. Not supported fortransformWithStateInPandasor the Scala API. - State operations that yield collections (such as
AsyncListState.getorAsyncMapState.keys) return async iterators that must be consumed usingasync for.
- structured streaming
- transformwithstate
- pyspark
- stateful processing
- lakeflow pipelines
Asynchronous processing with transformWithState is now generally available
Resumo e explicação gerados por IA a partir da release note oficial. Em caso de dúvida, a documentação oficial da plataforma prevalece.
Relacionadas
- DatabricksBeta
Conector do SurveyMonkey chega ao Lakeflow Connect no Databricks
O Lakeflow Connect agora conta com conector nativo para o SurveyMonkey em Beta, permitindo ingestão incremental de pesquisas, respostas e contatos com governança no Unity Catalog.
lakeflow connect · surveymonkey - DatabricksBeta
Lakeflow Connect adiciona conector do Oracle Eloqua em Beta
O Lakeflow Connect agora oferece um conector gerenciado em Beta para o Oracle Eloqua, permitindo ingestão incremental de dados de marketing governados pelo Unity Catalog.
lakeflow connect · oracle eloqua - DatabricksBeta
Ingira métricas do GA4 com o novo conector do Lakeflow Connect
O Lakeflow Connect ganha conector gerenciado em Beta para o Google Analytics 4, permitindo ingestão incremental de relatórios agregados via API com governança no Unity Catalog.
lakeflow connect · google analytics