Skip to content

Commit d6166b0

Browse files
committed
Deployed 5722873 with MkDocs version: 1.6.1
1 parent c6d6688 commit d6166b0

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

index.html

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,8 +2395,7 @@ <h3 id="io密集型任务">I/O密集型任务<a class="headerlink" href="#io密
23952395
<li>如果路由定义为 <code>async</code>,则会通过 <code>await</code> 正常调用,<br />
23962396
FastAPI 会信任你只执行非阻塞的 I/O 操作。</li>
23972397
</ul>
2398-
<p>但问题是,如果你辜负了这份信任,在异步路由中执行了阻塞操作,<br />
2399-
那么事件循环将无法继续执行其他任务,直到该阻塞操作完成。</p>
2398+
<p>需要注意的是,如果你辜负了这份信任, 并在异步路由中执行阻塞操作,则事件循环将无法运行后续任务,直到阻塞操作完成。</p>
24002399
<div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>
24012400
<span class="kn">import</span><span class="w"> </span><span class="nn">time</span>
24022401

@@ -2475,8 +2474,8 @@ <h3 id="io密集型任务">I/O密集型任务<a class="headerlink" href="#io密
24752474
<li>If the route is defined <code>async</code> then it's called regularly via <code>await</code>
24762475
and FastAPI trusts you to do only non-blocking I/O operations.</li>
24772476
</ul>
2478-
<p>The caveat is if you fail that trust and execute blocking operations within async routes,
2479-
the event loop will not be able to run the next tasks until that blocking operation is done.</p>
2477+
<p>The caveat is that if you violate that trust and execute blocking operations within async routes,
2478+
the event loop will not be able to run subsequent tasks until the blocking operation completes.</p>
24802479
<div class="highlight"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">asyncio</span>
24812480
<span class="kn">import</span><span class="w"> </span><span class="nn">time</span>
24822481

@@ -2902,8 +2901,7 @@ <h3 id="链式依赖">链式依赖<a class="headerlink" href="#链式依赖" tit
29022901
<div class="tabbed-set tabbed-alternate" data-tabs="10:2"><input checked="checked" id="__tabbed_10_1" name="__tabbed_10" type="radio" /><input id="__tabbed_10_2" name="__tabbed_10" type="radio" /><div class="tabbed-labels"><label for="__tabbed_10_1">中文</label><label for="__tabbed_10_2">英文</label></div>
29032902
<div class="tabbed-content">
29042903
<div class="tabbed-block">
2905-
<p>如果我们没有将数据验证放到依赖关系中,我们将不得不在每个端点中验证 <code>post_id</code> 是否存在,并为每个端点编写相同的测试。</p>
2906-
<p>依赖关系可以使用其他依赖关系,避免类似逻辑的代码重复。</p>
2904+
<p>依赖项可以使用其他依赖项,并避免类似逻辑的代码重复。</p>
29072905
<div class="highlight"><pre><span></span><code><span class="c1"># dependencies.py</span>
29082906
<span class="kn">from</span><span class="w"> </span><span class="nn">fastapi.security</span><span class="w"> </span><span class="kn">import</span> <span class="n">OAuth2PasswordBearer</span>
29092907
<span class="kn">from</span><span class="w"> </span><span class="nn">jose</span><span class="w"> </span><span class="kn">import</span> <span class="n">JWTError</span><span class="p">,</span> <span class="n">jwt</span>
@@ -2943,7 +2941,7 @@ <h3 id="链式依赖">链式依赖<a class="headerlink" href="#链式依赖" tit
29432941
</code></pre></div>
29442942
</div>
29452943
<div class="tabbed-block">
2946-
<p>Dependencies can use other dependencies and avoid code repetition for the similar logic.</p>
2944+
<p>Dependencies can use other dependencies and avoid code repetition for the the similar logic.</p>
29472945
<div class="highlight"><pre><span></span><code><span class="c1"># dependencies.py</span>
29482946
<span class="kn">from</span><span class="w"> </span><span class="nn">fastapi.security</span><span class="w"> </span><span class="kn">import</span> <span class="n">OAuth2PasswordBearer</span>
29492947
<span class="kn">from</span><span class="w"> </span><span class="nn">jose</span><span class="w"> </span><span class="kn">import</span> <span class="n">JWTError</span><span class="p">,</span> <span class="n">jwt</span>
@@ -3159,7 +3157,7 @@ <h3 id="遵循-rest">遵循 REST<a class="headerlink" href="#遵循-rest" title=
31593157
<li><code>GET /courses/:course_id/chapters/:chapter_id/lessons</code></li>
31603158
<li><code>GET /chapters/:chapter_id</code></li>
31613159
</ol>
3162-
<p>唯一需要注意的是路径中使用相同的变量名</p>
3160+
<p>唯一需要注意的是必须在路径中使用相同的变量名</p>
31633161
<ul>
31643162
<li>如果你有两个端点 <code>GET /profiles/:profile_id</code><code>GET /creators/:creator_id</code><br />
31653163
它们都验证给定的 <code>profile_id</code> 是否存在,但 <code>GET /creators/:creator_id</code> 还会检查该个人是否为创作者,<br />
@@ -3201,7 +3199,7 @@ <h3 id="遵循-rest">遵循 REST<a class="headerlink" href="#遵循-rest" title=
32013199
2. `GET /courses/:course_id/chapters/:chapter_id/lessons`
32023200
3. `GET /chapters/:chapter_id`
32033201
</code></pre></div>
3204-
<p>The only caveat is to use the same variable names in the path:</p>
3202+
<p>The only caveat is having to use the same variable names in the path:</p>
32053203
<ul>
32063204
<li>If you have two endpoints <code>GET /profiles/:profile_id</code> and <code>GET /creators/:creator_id</code>
32073205
that both validate whether the given <code>profile_id</code> exists, but <code>GET /creators/:creator_id</code>
@@ -3244,9 +3242,13 @@ <h3 id="fastapi-响应序列化">FastAPI 响应序列化<a class="headerlink" hr
32443242
<div class="tabbed-set tabbed-alternate" data-tabs="14:2"><input checked="checked" id="__tabbed_14_1" name="__tabbed_14" type="radio" /><input id="__tabbed_14_2" name="__tabbed_14" type="radio" /><div class="tabbed-labels"><label for="__tabbed_14_1">中文</label><label for="__tabbed_14_2">英文</label></div>
32453243
<div class="tabbed-content">
32463244
<div class="tabbed-block">
3247-
<p>如果你认为可以返回与路由的 <code>response_model</code> 匹配的 Pydantic 对象以进行优化,那么这是错误的。</p>
3248-
<p>FastAPI 首先使用 <code>jsonable_encoder</code> 将该 Pydantic 对象转换为字典,然后使用你的 <code>response_model</code> 验证数据,<br />
3249-
最后才将对象序列化为 JSON。</p>
3245+
<p>您可能认为您可以返回与您的路线的 <code>response_model</code> 匹配的 Pydantic 对象来进行一些优化,但您错了。</p>
3246+
<p>FastAPI 首先使用其 <code>jsonable_encoder</code> 将该 pydantic 对象转换为 dict,然后使用 <code>response_model</code> 验证数据,最后才将您的对象序列化为 JSON。</p>
3247+
<p>这意味着您的 Pydantic 模型对象被创建了两次:</p>
3248+
<ul>
3249+
<li>首先,当您明确创建它以从您的路由返回时。</li>
3250+
<li>其次,FastAPI 隐式地根据 request_model 验证响应数据。</li>
3251+
</ul>
32503252
<div class="highlight"><pre><span></span><code><span class="kn">from</span><span class="w"> </span><span class="nn">fastapi</span><span class="w"> </span><span class="kn">import</span> <span class="n">FastAPI</span>
32513253
<span class="kn">from</span><span class="w"> </span><span class="nn">pydantic</span><span class="w"> </span><span class="kn">import</span> <span class="n">BaseModel</span><span class="p">,</span> <span class="n">root_validator</span>
32523254

@@ -3271,10 +3273,15 @@ <h3 id="fastapi-响应序列化">FastAPI 响应序列化<a class="headerlink" hr
32713273
</code></pre></div>
32723274
</div>
32733275
<div class="tabbed-block">
3274-
<p>If you think you can return Pydantic object that matches your route's <code>response_model</code> to make some optimizations,
3275-
then it's wrong.</p>
3276-
<p>FastAPI firstly converts that pydantic object to dict with its <code>jsonable_encoder</code>, then validates
3276+
<p>You may think you can return Pydantic object that matches your route's <code>response_model</code> to make some optimizations,
3277+
but you'd be wrong.</p>
3278+
<p>FastAPI first converts that pydantic object to dict with its <code>jsonable_encoder</code>, then validates
32773279
data with your <code>response_model</code>, and only then serializes your object to JSON.</p>
3280+
<p>This means your Pydantic model object is created twice:</p>
3281+
<ul>
3282+
<li>First, when you explicitly create it to return from your route.</li>
3283+
<li>Second, implicitly by FastAPI to validate the response data according to the response_model.</li>
3284+
</ul>
32783285
<div class="highlight"><pre><span></span><code><span class="kn">from</span><span class="w"> </span><span class="nn">fastapi</span><span class="w"> </span><span class="kn">import</span> <span class="n">FastAPI</span>
32793286
<span class="kn">from</span><span class="w"> </span><span class="nn">pydantic</span><span class="w"> </span><span class="kn">import</span> <span class="n">BaseModel</span><span class="p">,</span> <span class="n">root_validator</span>
32803287

search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

sitemap.xml.gz

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)