Skip to content

Instantly share code, notes, and snippets.

@virattt
Created January 26, 2024 23:05
Show Gist options
  • Save virattt/339a1ce1fd49efb0d665f9dbc9ad685f to your computer and use it in GitHub Desktop.
Save virattt/339a1ce1fd49efb0d665f9dbc9ad685f to your computer and use it in GitHub Desktop.
concurrent-vs-sequential-rag.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOAmluhWUme3P2wdFRqdvh8",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/virattt/339a1ce1fd49efb0d665f9dbc9ad685f/concurrent-vs-sequential-rag.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"import requests\n",
"\n",
"apiKey = \"YOUR_POLYGON_API_KEY\""
],
"metadata": {
"id": "vFaIzVST7kuk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Define sequential API code"
],
"metadata": {
"id": "drQiGAA4kLsy"
}
},
{
"cell_type": "code",
"source": [
"import requests\n",
"import time\n",
"\n",
"def get_latest_price_seq(ticker: str) -> dict:\n",
" response = requests.get(f\"https://api.polygon.io/v2/last/nbbo/{ticker}?apiKey={apiKey}\")\n",
" return response.json()\n",
"\n",
"def get_latest_news_seq(ticker: str) -> dict:\n",
" response = requests.get(f\"https://api.polygon.io/v2/reference/news?ticker={ticker}&apiKey={apiKey}\")\n",
" return response.json()\n",
"\n",
"def query_data_sources_seq(ticker: str) -> dict:\n",
" latest_price = get_latest_price_seq(ticker)\n",
" latest_news = get_latest_news_seq(ticker)\n",
"\n",
" return {\n",
" 'latest_price': latest_price,\n",
" 'latest_news': latest_news,\n",
" }\n"
],
"metadata": {
"id": "XqLfPPIqASA5"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Define concurrent API code"
],
"metadata": {
"id": "u8Zhy1NjkOe5"
}
},
{
"cell_type": "code",
"source": [
"!pip install httpx"
],
"metadata": {
"id": "BIM3ALyoNm4l"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import asyncio\n",
"import httpx\n",
"\n",
"async def get_latest_price_conc(ticker: str) -> dict:\n",
" async with httpx.AsyncClient() as client:\n",
" response = await client.get(f\"https://api.polygon.io/v2/last/nbbo/{ticker}?apiKey={apiKey}\")\n",
" return response.json()\n",
"\n",
"async def get_latest_news_conc(ticker: str) -> dict:\n",
" async with httpx.AsyncClient() as client:\n",
" response = await client.get(f\"https://api.polygon.io/v2/reference/news?ticker={ticker}&apiKey={apiKey}\")\n",
" return response.json()\n",
"\n",
"async def query_data_sources_conc(ticker: str) -> dict:\n",
" results = await asyncio.gather(\n",
" get_latest_price_conc(ticker),\n",
" get_latest_news_conc(ticker),\n",
" )\n",
" return {\n",
" 'latest_price': results[0],\n",
" 'latest_news': results[1],\n",
" }\n"
],
"metadata": {
"id": "6i56IHZvB1Hl"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Compute avg. times (sequential vs. concurrent)"
],
"metadata": {
"id": "TWdF6eEBhM15"
}
},
{
"cell_type": "code",
"source": [
"!pip install nest_asyncio"
],
"metadata": {
"id": "DYBkvPIpjrOb"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import nest_asyncio\n",
"\n",
"def measure_time_sequential(ticker: str, runs: int = 100) -> float:\n",
" total_time = 0\n",
" for _ in range(runs):\n",
" start_time = time.time()\n",
" query_data_sources_seq(ticker)\n",
" total_time += time.time() - start_time\n",
" return total_time / runs\n",
"\n",
"async def measure_time_concurrent(ticker: str, runs: int = 100) -> float:\n",
" total_time = 0\n",
" for _ in range(runs):\n",
" start_time = time.time()\n",
" await query_data_sources_conc(ticker)\n",
" total_time += time.time() - start_time\n",
" return total_time / runs\n",
"\n",
"# Apply the nest_asyncio patch\n",
"nest_asyncio.apply()\n",
"\n",
"# Measuring the times\n",
"ticker = \"AAPL\" # Replace with your desired ticker\n",
"avg_time_seq = measure_time_sequential(ticker)\n",
"\n",
"# For concurrent measurement, now you can use asyncio.run without issues\n",
"avg_time_conc = asyncio.run(measure_time_concurrent(ticker))\n",
"\n",
"print(f\"Average time for sequential calls: {avg_time_seq} seconds\")\n",
"print(f\"Average time for concurrent calls: {avg_time_conc} seconds\")\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "reMllZB6FB9v",
"outputId": "18bd8b45-0d7c-498b-fe15-2a16464e6793"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Average time for sequential calls: 0.5349038124084473 seconds\n",
"Average time for concurrent calls: 0.38312270641326907 seconds\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "YDJXOPc9kZFD"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment