<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Fastapi on Rainux</title>
    <link>https://gh.heruos.top/en/tags/fastapi/</link>
    <description>Recent content in Fastapi on Rainux</description>
    <generator>Hugo -- 0.152.2</generator>
    <language>en</language>
    <lastBuildDate>Sun, 08 Feb 2026 23:56:57 +0800</lastBuildDate>
    <atom:link href="https://gh.heruos.top/en/tags/fastapi/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Executing Asynchronous Scheduled Tasks with Database-Based Distributed Locking in FastAPI</title>
      <link>https://gh.heruos.top/en/post/2026/fastapi-schedule-async-job-with-db-lock/</link>
      <pubDate>Sun, 08 Feb 2026 23:56:57 +0800</pubDate>
      <guid>https://gh.heruos.top/en/post/2026/fastapi-schedule-async-job-with-db-lock/</guid>
      <description>This article provides a comprehensive guide on implementing an asynchronous task scheduler in FastAPI, using database-based distributed locks to prevent duplicate task execution in multi-process environments.</description>
    </item>
    <item>
      <title>FastAPI - Calling Synchronous Methods in FastAPI Asynchronous Methods</title>
      <link>https://gh.heruos.top/en/post/2026/01-06-fastapi_calling_sync_method_in_async_method/</link>
      <pubDate>Tue, 06 Jan 2026 16:19:44 +0800</pubDate>
      <guid>https://gh.heruos.top/en/post/2026/01-06-fastapi_calling_sync_method_in_async_method/</guid>
      <description>&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Directly calling synchronous methods within asynchronous methods blocks the entire event loop, preventing the application from handling any other concurrent requests while executing the synchronous method. This severely impacts the overall performance and responsiveness of the service.&lt;/p&gt;
&lt;p&gt;To address this issue, the core approach is to delegate synchronous methods to external thread pools or process pools for execution, thereby avoiding blocking the main event loop.&lt;/p&gt;
&lt;h2 id=&#34;method-1-using-asyncioto_thread&#34;&gt;Method 1: Using asyncio.to_thread&lt;/h2&gt;
&lt;p&gt;Python 3.9 and later versions provide the &lt;code&gt;asyncio.to_thread&lt;/code&gt; method, which runs synchronous functions in a separate thread and returns a coroutine object that can be awaited.&lt;/p&gt;</description>
    </item>
    <item>
      <title>FastAPI - Design of tracking_id</title>
      <link>https://gh.heruos.top/en/post/2025/fastapi_design_of_tracking_id/</link>
      <pubDate>Thu, 11 Dec 2025 00:40:44 +0800</pubDate>
      <guid>https://gh.heruos.top/en/post/2025/fastapi_design_of_tracking_id/</guid>
      <description>&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In real business scenarios, tracing a request&amp;rsquo;s complete processing path in logs based on a &lt;code&gt;tracking_id&lt;/code&gt; is a common requirement. However, FastAPI does not provide this functionality out of the box, so developers need to implement it themselves. This article introduces how to add a tracking_id to the entire request lifecycle based on &lt;code&gt;contextvars&lt;/code&gt; and automatically record it in logs.&lt;/p&gt;
&lt;h2 id=&#34;what-is-contextvars&#34;&gt;What is contextvars&lt;/h2&gt;
&lt;p&gt;Python 3.7 added a module &lt;code&gt;contextvars&lt;/code&gt; to the standard library, which stands for &amp;ldquo;Context Variables&amp;rdquo;. It is typically used to implicitly pass some environmental information variables, similar to &lt;code&gt;threading.local()&lt;/code&gt;. However, &lt;code&gt;threading.local()&lt;/code&gt; is thread-specific, isolating data states between threads, while &lt;code&gt;contextvars&lt;/code&gt; can be used in asynchronous coroutines within the &lt;code&gt;asyncio&lt;/code&gt; ecosystem. &lt;strong&gt;PS: &lt;code&gt;contextvars&lt;/code&gt; can not only be used in asynchronous coroutines but can also replace &lt;code&gt;threading.local()&lt;/code&gt; in multi-threaded functions.&lt;/strong&gt;&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
