Python Async API#
Use vl_convert.asyncio when the caller already runs an asyncio event loop, such as an asynchronous web service. Its conversion and configuration functions are awaitable, so the event loop keeps serving other tasks while VlConvert works.
The synchronous and asynchronous modules share one converter and worker pool. Configure that shared state once at startup, before concurrent conversions begin.
import asyncio
from pathlib import Path
import vl_convert.asyncio as vlca
spec = {
"data": {"values": [{"category": "A", "value": 2}, {"category": "B", "value": 5}]},
"mark": "bar",
"encoding": {
"x": {"field": "category", "type": "nominal"},
"y": {"field": "value", "type": "quantitative"},
},
}
async def main():
await vlca.configure(num_workers=2)
await vlca.warm_up_workers()
png, svg = await asyncio.gather(
vlca.vegalite_to_png(spec, scale=2),
vlca.vegalite_to_svg(spec),
)
Path("chart.png").write_bytes(png)
Path("chart.svg").write_text(svg, encoding="utf-8")
asyncio.run(main())
A pool runs as many conversions at once as it has workers, and extra calls wait for a free worker. See Memory and Execution Limits.
See the Python Async API Reference for which vl_convert.asyncio functions are awaitable and which are synchronous.