Async vs Sync Usage in GraphBit Python API
GraphBit's Python bindings provide both synchronous (blocking) and asynchronous (non-blocking) interfaces for key operations, allowing you to choose the best fit for your application—whether it's a quick script, a web server, or a data pipeline.
Overview
- Synchronous (Sync) functions block the current thread until the operation completes. They are simple to use in scripts and REPLs.
- Asynchronous (Async) functions return immediately and run in the background, allowing your program to do other work or handle many tasks concurrently. Async is ideal for web servers, pipelines, and high-throughput applications.
Supported Sync and Async Functions
Module
LLM Client
Embedding Client
Workflow
Workflow Executor
Workflow Result
Usage Examples
Synchronous Usage
import os
from graphbit import LlmConfig, LlmClient
config = LlmConfig.openai(os.getenv("OPENAI_API_KEY"))
client = LlmClient(config)
result = client.complete("Hello, world!")
print(result)
Asynchronous Usage
import os
import asyncio
from graphbit import LlmConfig, LlmClient
config = LlmConfig.openai(os.getenv("OPENAI_API_KEY"))
client = LlmClient(config)
async def main():
result = await client.complete_async("Hello, async world!")
print(result)
# Streaming completion
stream_result = await client.complete_stream("Stream this!")
print(stream_result)
asyncio.run(main())
This documentation demonstrates sync and async functions supported by GraphBit’s Python API, enabling you to select the best approach for your use case, whether you need straightforward synchronous calls or high-performance asynchronous workflows.