GnuplotEx.Async (gnuplot_ex v0.5.1)

Async rendering functions for GnuplotEx.

Provides non-blocking alternatives to synchronous render functions, enabling parallel plot generation and real-time streaming use cases.

Examples

# Single async render
task = GnuplotEx.Async.render(plot, :svg)
{:ok, svg} = Task.await(task)

# Parallel multi-plot rendering
plots = [plot1, plot2, plot3]
results = GnuplotEx.Async.render_many(plots, :svg, max_concurrency: 4)

# Fire-and-forget with callback
GnuplotEx.Async.render_callback(plot, :svg, fn
  {:ok, svg} -> push_to_client(svg)
  {:error, reason} -> log_error(reason)
end)

Summary

Functions

Await multiple render tasks with a timeout.

Render a plot asynchronously, returning a Task.

Render with callback on completion.

Render multiple plots in parallel using Task.async_stream.

Functions

await_many(tasks, timeout \\ 5000)

@spec await_many([Task.t()], timeout()) :: [term()]

Await multiple render tasks with a timeout.

Helper for managing multiple async renders started with render/3.

Example

tasks = Enum.map(plots, &GnuplotEx.Async.render/1)
results = GnuplotEx.Async.await_many(tasks, 30_000)

render(plot, format \\ :svg, opts \\ [])

@spec render(GnuplotEx.Plot.t(), atom(), keyword()) :: Task.t()

Render a plot asynchronously, returning a Task.

The task can be awaited with Task.await/2 or monitored for completion.

Options

All options are passed through to GnuplotEx.render/3.

Example

task = GnuplotEx.Async.render(plot, :svg)
# ... do other work ...
{:ok, svg} = Task.await(task)

render_callback(plot, format, callback, opts \\ [])

@spec render_callback(GnuplotEx.Plot.t(), atom(), (term() -> any()), keyword()) ::
  {:ok, pid()}

Render with callback on completion.

Useful for fire-and-forget scenarios or when integrating with message-passing systems. The callback receives the render result.

Example

GnuplotEx.Async.render_callback(plot, :svg, fn
  {:ok, svg} -> send(pid, {:plot_ready, svg})
  {:error, reason} -> Logger.error("Render failed: #{reason}")
end)
# Returns immediately with {:ok, pid}

Options

All options are passed through to GnuplotEx.render/3.

render_many(plots, format \\ :svg, opts \\ [])

@spec render_many([GnuplotEx.Plot.t()], atom(), keyword()) :: [
  ok: binary(),
  error: term()
]

Render multiple plots in parallel using Task.async_stream.

Returns a list of results in the same order as the input plots (unless ordered: false is specified).

Options

  • :max_concurrency - Maximum concurrent renders (default: System.schedulers_online())
  • :timeout - Per-plot timeout in ms (default: 30_000)
  • :ordered - Return results in order (default: true)

All other options are passed through to GnuplotEx.render/3.

Example

plots = [plot1, plot2, plot3]
results = GnuplotEx.Async.render_many(plots, :svg, max_concurrency: 4)
# => [{:ok, svg1}, {:ok, svg2}, {:ok, svg3}]

# Handle mixed results
Enum.each(results, fn
  {:ok, svg} -> save(svg)
  {:error, reason} -> log_error(reason)
end)