Async Patterns
Best practices for async usage - concurrent operations, error handling, connection pooling
Prerequisites
# Install the SDK with async support
pip install seismic-web3
# Set environment variables
export PRIVATE_KEY="your_64_char_hex_private_key"
export RPC_URL="https://gcp-1.seismictest.net/rpc"
export WS_URL="wss://gcp-1.seismictest.net/ws"
export CONTRACT_ADDRESS="0x..."Basic Async Client Setup
import asyncio
import os
from seismic_web3 import create_async_wallet_client, PrivateKey
async def main():
private_key = PrivateKey.from_hex_str(os.environ["PRIVATE_KEY"])
# Create async client
w3 = await create_async_wallet_client(
os.environ["RPC_URL"],
private_key=private_key,
)
try:
# All operations must be awaited
chain_id = await w3.eth.chain_id
block_number = await w3.eth.block_number
print(f"Connected to chain {chain_id}, block {block_number}")
finally:
# Always disconnect to clean up resources
await w3.provider.disconnect()
if __name__ == "__main__":
asyncio.run(main())Concurrent Operations
Concurrent Writes with Progress Tracking
Error Handling with Retries
Context Manager Pattern
WebSocket Connection for Events
Connection Pooling Pattern
Timeout Handling
Rate Limiting
Complete Production-Ready Pattern
Expected Output
Best Practices
Next Steps
See Also
Last updated

