<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Harshil Jani</title>
    <description>The latest articles on Forem by Harshil Jani (@harshil_jani).</description>
    <link>https://forem.com/harshil_jani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F658002%2F9d3a97f6-f5c7-4fa2-a521-a42f7ee5169a.png</url>
      <title>Forem: Harshil Jani</title>
      <link>https://forem.com/harshil_jani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/harshil_jani"/>
    <language>en</language>
    <item>
      <title>Introducing fastapi-bgtasks-dashboard : One-liner integration on your FastAPI application</title>
      <dc:creator>Harshil Jani</dc:creator>
      <pubDate>Wed, 24 Sep 2025 20:40:15 +0000</pubDate>
      <link>https://forem.com/harshil_jani/introducing-fastapi-bgtasks-dashboard-one-liner-integration-on-your-fastapi-application-343i</link>
      <guid>https://forem.com/harshil_jani/introducing-fastapi-bgtasks-dashboard-one-liner-integration-on-your-fastapi-application-343i</guid>
      <description>&lt;p&gt;FastAPI is a premier framework for constructing high-performance APIs, favored by tech giants, fintech firms, and e-commerce platforms for its asynchronous capabilities and efficiency. &lt;/p&gt;

&lt;p&gt;One of its standout features is the &lt;code&gt;BackgroundTasks&lt;/code&gt; class, which facilitates "fire-and-forget" operations such as email dispatching, data processing, or external API integrations. These tasks run post-response, ensuring non-blocking user experiences.&lt;/p&gt;

&lt;p&gt;However, tracking it is not possible without having excessive logs. Constant log monitoring becomes impractical at scale, leading to opaque systems where tasks may fail silently, consume excessive resources, or inflate cloud costs.&lt;/p&gt;

&lt;p&gt;To address this problem I've developed &lt;code&gt;fastapi-bgtasks-dashboard&lt;/code&gt;, an open-source Python package that integrates a real-time dashboard into your FastAPI application with minimal effort. It's just a single line of change on your application which enables the entire real-time dashboard which trackes the background tasks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx3675xiluhfyhjwpcenj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx3675xiluhfyhjwpcenj.png" alt="Demo of fastapi-bgtasks-dashboard" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Integration and Setup
&lt;/h1&gt;

&lt;p&gt;Incorporating the dashboard is straightforward, requiring just one line of code after installation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install fastapi-bgtasks-dashboard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your main application file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI
from fastapi_bgtasks_dashboard import mount_bg_tasks_dashboard

app = FastAPI()

# Integrate the dashboard effortlessly
mount_bg_tasks_dashboard(app=app)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Launch your server (e.g., via &lt;code&gt;uvicorn main:app --reload&lt;/code&gt;), and navigate to &lt;code&gt;http://localhost:8000/dashboard&lt;/code&gt;. The interface instantly populates with task details as they execute.&lt;/p&gt;

&lt;p&gt;Under the hood, the package leverages FastAPI's dependency injection to capture tasks added through BackgroundTasks. It records essential metadata without altering your existing codebase. For example, consider a typical endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BackgroundTasks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;

&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/analyze&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;analyze_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;background_tasks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BackgroundTasks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;background_tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heavy_computation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Processing initiated&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;heavy_computation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Simulate intensive processing
&lt;/span&gt;    &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Replace with actual logic
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Processed data: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Triggering this endpoint queues the task, which appears in the dashboard with attributes like start time, duration (formatted in ms/s/m/h), parameters, status, and any exceptions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Technical Features
&lt;/h1&gt;

&lt;p&gt;It is already designed for enterprise-scale applications, the features that dashboard offers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Real-Time Monitoring:&lt;/strong&gt; Utilizes WebSockets for live updates, ensuring instantaneous visibility into task progress without page reloads. This is built on Starlette's async infrastructure, aligning perfectly with FastAPI's ecosystem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Interactive Controls:&lt;/strong&gt; Sort and filter tasks by function name, status, or duration. Re-execute failed tasks with a single click, preserving original parameters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Efficient Storage:&lt;/strong&gt; Defaults to an in-memory, thread-safe dictionary, optimized for handling millions of tasks on modest hardware (1-2 GB RAM suffices, with metadata footprint in mere MBs). A "clear tasks" feature allows manual flushing to prevent memory bloat, ideal for long-running services.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without such a tool, applications risk undetected inefficiencies—tasks could deadlock, leak memory, or violate SLAs in distributed environments like Kubernetes clusters.&lt;/p&gt;

&lt;h1&gt;
  
  
  Future Development Roadmap
&lt;/h1&gt;

&lt;p&gt;Version 0.1.5 represents a stable release, with all known issues resolved. It focuses on core functionality without external dependencies.&lt;/p&gt;

&lt;p&gt;Upcoming iterations will introduce persistent storage options, targeting Redis for caching in distributed systems and PostgreSQL for robust querying and historical retention.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why Adopt This Tool?
&lt;/h1&gt;

&lt;p&gt;For teams managing critical FastAPI systems such as real-time analytics or payment gateways—this dashboard mitigates blind spots, reduces debugging overhead, and optimizes resource utilization. It prevents production meltdowns and curtails unnecessary cloud expenditures, fostering reliable, scalable architectures.&lt;/p&gt;

&lt;p&gt;Explore the full documentation on &lt;a href="https://pypi.org/project/fastapi-bgtasks-dashboard/" rel="noopener noreferrer"&gt;PyPI&lt;/a&gt; or the &lt;a href="https://github.com/Harshil-Jani/fastapi-bgtasks-dashboard" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; repository. I encourage starring the repo to support visibility and contributing via pull requests—whether enhancing integrations, adding metrics (e.g., Prometheus), or refining the UI.&lt;/p&gt;

&lt;p&gt;My vision is to integrate this into the official FastAPI ecosystem, and improve the background task handling industry-wide. Let's collaborate to make FastAPI applications more resilient and observable. Feedback and experiences with background task challenges are welcome in the comments!&lt;/p&gt;

</description>
      <category>python</category>
      <category>fastapi</category>
      <category>backend</category>
      <category>development</category>
    </item>
    <item>
      <title>NumPy’s SIMD-Friendly Design Boosts Performance Over Python Lists</title>
      <dc:creator>Harshil Jani</dc:creator>
      <pubDate>Sat, 06 Sep 2025 23:25:50 +0000</pubDate>
      <link>https://forem.com/harshil_jani/numpys-simd-friendly-design-boosts-performance-over-python-lists-473o</link>
      <guid>https://forem.com/harshil_jani/numpys-simd-friendly-design-boosts-performance-over-python-lists-473o</guid>
      <description>&lt;h1&gt;
  
  
  NumPy’s SIMD-Friendly Design Boosts Performance Over Python Lists
&lt;/h1&gt;

&lt;p&gt;When optimizing Python code for speed, especially in data-heavy applications like machine learning or analytics, the choice of data structure matters a lot.&lt;/p&gt;

&lt;p&gt;Python lists are slow in comparison to NumPy arrays for numerical tasks. Its use of contiguous memory makes SIMD (Single Instruction, Multiple Data) vector processing which is a hardware feature that processes multiple data elements in parallel much faster.&lt;/p&gt;

&lt;p&gt;In this post, we’ll explore why NumPy’s design delivers massive performance improvements over Python lists, with simple explanations, a clear code example and benchmarks to prove it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Memory Layout Matters
&lt;/h2&gt;

&lt;p&gt;The difference between NumPy arrays and Python lists is how they store data:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcrgjdtfwvw4bltafgk2v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcrgjdtfwvw4bltafgk2v.png" alt="Medium-Image" width="640" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Python Lists (Scattered)&lt;/strong&gt; : Each element is a separate Python object, stored at potentially different memory addresses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NumPy Arrays (Contiguous)&lt;/strong&gt; : Data is stored in a single, continuous block of memory. This allows the CPU to grab chunks of data efficiently.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;SIMD utilizes contiguous memory because it can load several values (e.g., 4 or 8 floats) into a vector register with one instruction. Scattered memory, like in Python lists, forces the CPU to access elements individually, preventing SIMD and causing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cache Misses&lt;/strong&gt; : Scattered data misses the CPU’s fast cache, fetching from slower main memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Parallelism&lt;/strong&gt; : Individual accesses block SIMD, reducing throughput.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extra Overhead&lt;/strong&gt; : Pointer chasing in scattered memory adds latency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;NumPy’s contiguous layout aligns perfectly with SIMD, enabling faster, parallel processing.&lt;/p&gt;
&lt;h2&gt;
  
  
  A Simple Benchmark to Show the Difference
&lt;/h2&gt;

&lt;p&gt;Let’s test this with a basic operation by adding a constant to 100,000 numbers. We’ll compare a NumPy array to a Python list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import numpy as np
import time

# Setup: 100,000 elements
size = 100_000
numpy_array = np.ones(size, dtype=np.float32)  # Contiguous
python_list = [1.0] * size  # Scattered

# Operation: Add 5 to each element
constant = 5.0

# NumPy (SIMD-friendly)
start = time.time()
numpy_result = numpy_array + constant
numpy_time = time.time() - start

# Python List (Scattered)
start = time.time()
python_result = [x + constant for x in python_list]
python_time = time.time() - start

# Results
print(f&amp;amp;quot;NumPy Array Time: {numpy_time:.6f} seconds&amp;amp;quot;)
print(f&amp;amp;quot;Python List Time: {python_time:.6f} seconds&amp;amp;quot;)
print(f&amp;amp;quot;NumPy Speedup: {python_time / numpy_time:.2f}x&amp;amp;quot;)
print(f&amp;amp;quot;NumPy result sample: {numpy_result[:5]}&amp;amp;quot;)
print(f&amp;amp;quot;Python result sample: {python_result[:5]}&amp;amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sample Output&lt;/strong&gt;  (varies by system):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NumPy Array Time: 0.000306 seconds
Python List Time: 0.010526 seconds
NumPy Speedup: 34.36x
NumPy result sample: [6. 6. 6. 6. 6.]
Python result sample: [6.0, 6.0, 6.0, 6.0, 6.0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NumPy is &lt;strong&gt;~34x faster&lt;/strong&gt;  here because its contiguous memory enables SIMD to process multiple elements per CPU cycle, while Python lists require slow, sequential access.&lt;/p&gt;

&lt;p&gt;In a real-world application like processing millions of records in a data pipeline this speedup can mean the difference between a snappy service and one that struggles under load.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is It Just for Integers? Supporting Other Data Types
&lt;/h2&gt;

&lt;p&gt;NumPy is optimized for homogeneous data, so all elements in an array must be of the same type. This uniformity is what enables SIMD and contiguous memory benefits.&lt;/p&gt;

&lt;p&gt;Python lists can hold mixed types (e.g., integers, strings, objects) but lack the performance optimization due to their scattered memory addresses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use NumPy Arrays When&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Working with large, homogeneous numerical data (e.g., integers, floats) for performance-critical tasks.&lt;/li&gt;
&lt;li&gt;Needing vectorized operations (e.g., matrix multiplication, statistical computations).&lt;/li&gt;
&lt;li&gt;Integrating with numerical libraries (e.g., SciPy, Pandas).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Python Lists When&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dealing with small datasets or mixed data types (e.g., a list of [1, “text”, 3.14]).&lt;/li&gt;
&lt;li&gt;Prototyping non-numerical logic or needing dynamic resizing.&lt;/li&gt;
&lt;li&gt;Example: Storing configuration settings or a small collection of diverse objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For mixed-type or small-scale tasks, Python lists are more flexible. For numerical performance, especially with SIMD, NumPy’s typed arrays are the way to go. You can always convert a list to a NumPy array with np.array(my_list, dtype=desired_type) to leverage these benefits.&lt;/p&gt;

&lt;p&gt;Encourage your team to experiment with NumPy in small tasks to see the benefits firsthand. A quick profiling session can highlight the performance wins. Do share your performance gains in the comments below.&lt;/p&gt;

</description>
      <category>python</category>
      <category>numpy</category>
      <category>performance</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
