Skip to content

Commit 469be87

Browse files
committed
update post
1 parent c26ae7e commit 469be87

File tree

11 files changed

+76
-18
lines changed

11 files changed

+76
-18
lines changed

content/posts/fastapi-body-advanced-uses.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ date = '2025-08-09T10:00:00+08:00'
33
draft = false
44
title = 'FastAPI Body Advanced Uses'
55
+++
6-
本篇文章介绍的 FastAPI Request Body 的进阶用法
6+
本篇文章介绍 FastAPI Request Body 的进阶用法
77

88
### Body - Multiple Parameters
99
首先, 可以将`Path`, `Query` 和 request body 参数声明自由的写在一起
@@ -178,7 +178,7 @@ async def update_item(
178178

179179

180180
### Body - Fields
181-
除了可以在*path* operation (路径操作)函数参数中使用 `Query``Path``Body`来声明额外的验证和数据, 还可以在 Pydantic 模型内部的 `Field` 的字段验证规则和元数据
181+
除了可以在 *path* operation (路径操作)函数参数中使用 `Query``Path``Body`来声明额外的验证和数据, 还可以在 Pydantic 模型内部的 `Field` 的字段验证规则和元数据
182182

183183
#### Declare model attributes 声明模型字段属性
184184
首先要导入 Filed

content/posts/python-asyncio.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
+++
2+
date = '2025-08-10T10:00:00+08:00'
3+
draft = true
4+
title = 'Python Asyncio Introduce'
5+
+++
6+
7+
### Fast Introduce
8+
下面代码中, 定义了一个 `main()` 协程函数, 然后通过 `asyncio.run()` 来执行它
9+
```Python
10+
import asyncio
11+
import time
12+
13+
async def main():
14+
print(f"{time.ctime()} Hello!")
15+
await asyncio.sleep(1.0)
16+
print(f"{time.ctime()} Goodbye!")
17+
18+
asyncio.run(main())
19+
```
20+
在实践中, 大多数基于 asyncio 的代码都会使用 `run()` 函数, 但是要了解实际发生了什么, 参考下面这段代码 (实现了同样的功能)
21+
```Python
22+
import asyncio
23+
import time
24+
25+
async def main():
26+
print(f"{time.ctime()} Hello!")
27+
await asyncio.sleep(1.0)
28+
print(f"{time.ctime()} Goodbye!")
29+
30+
loop = asyncio.get_event_loop() # 1
31+
task = loop.create_task(main()) # 2
32+
loop.run_until_complete(task) # 3
33+
pending = asyncio.all_tasks(loop=loop) # 4
34+
for task in pending: # 5
35+
task.cancel()
36+
group = asyncio.gather(*pending, return_exceptions=True) # 6
37+
38+
loop.run_until_complete(group) # 7
39+
loop.close() # 8
40+
```
41+
代码解释:
42+
1. 获取事件循环: 使用 `get_event_loop()` 创建一个**事件循环实例**(如果当前线程没有 loop 就会创建一个), 一个线程中, 多次调用 `get_running_loop()` 会返回同一个 loop 对象
43+
- 如果是在异步的 async def 函数中, 则应该调用 `get_running_loop()`
44+
45+
2. 把协程调度到循环中: 使用 `loop.create_task()` 将协程对象 `main()` 封装成 Task 对象, 并注册到事件循环中. Task 会让协程自动开始执行, 而不必手动 `await`
46+
- 返回的 `task` 可以用来: 查询任务状态(如 `.done()``.result()`), 取消任务(`task.cancel()`)
47+
48+
3. 启动事件循环并阻塞主线程: 阻塞当前进程, 直到 `task` 执行完成
49+
50+
4. 找出并取消剩余任务: 使用 `asyncio.all_tasks()` 获取这个 loop 里所有未完成的任务, 一般在程序收尾时做一次清理, 防止有任务卡住 loop
51+
52+
5. 给这些任务发取消信号
53+
54+
6. 收集所有任务并处理异常: `asyncio.gather()` 会返回一个**聚合任务**, 把所有 `pending` 任务捆绑成一个任务对象. `return_exceptions=True` 表示即使某个任务抛异常,也不会直接中断,会把异常作为结果返回
55+
56+
7. 再次运行事件循环, 直到所有 pending 任务完成(确保程序退出前 loop 是干净的)
57+
58+
8. 关闭事件循环

public/en/sitemap.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?xml version="1.0" encoding="utf-8" standalone="yes"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"><url><loc>https://starslayerx.github.io/posts/fastapi-body-advanced-uses/</loc><lastmod>2025-08-09T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/</loc><lastmod>2025-08-09T10:00:00+08:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/posts/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/posts/"/></url><url><loc>https://starslayerx.github.io/posts/git-whitelist/</loc><lastmod>2025-08-08T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-parameters-and-validations/</loc><lastmod>2025-08-07T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-parameters/</loc><lastmod>2025-08-06T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/python-tricks/</loc><lastmod>2025-08-05T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/executing-arbitrary-python-code-from-a-comment/</loc><lastmod>2025-08-04T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/how-fastapi-works/</loc><lastmod>2025-08-01T10:30:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/blaugust/</loc><lastmod>2025-08-01T10:00:00+08:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/posts/blaugust/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/posts/blaugust/"/></url><url><loc>https://starslayerx.github.io/</loc><lastmod>2023-01-01T08:00:00-07:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/"/></url><url><loc>https://starslayerx.github.io/archives/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/archives/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/archives/"/></url><url><loc>https://starslayerx.github.io/categories/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/categories/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/categories/"/></url><url><loc>https://starslayerx.github.io/tags/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/tags/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/tags/"/></url></urlset>
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"><url><loc>https://starslayerx.github.io/posts/</loc><lastmod>2025-08-10T10:00:00+08:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/posts/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/posts/"/></url><url><loc>https://starslayerx.github.io/posts/fastapi-body-advanced-uses/</loc><lastmod>2025-08-09T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/git-whitelist/</loc><lastmod>2025-08-08T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-parameters-and-validations/</loc><lastmod>2025-08-07T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/fastapi-parameters/</loc><lastmod>2025-08-06T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/python-tricks/</loc><lastmod>2025-08-05T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/executing-arbitrary-python-code-from-a-comment/</loc><lastmod>2025-08-04T10:00:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/how-fastapi-works/</loc><lastmod>2025-08-01T10:30:00+08:00</lastmod></url><url><loc>https://starslayerx.github.io/posts/blaugust/</loc><lastmod>2025-08-01T10:00:00+08:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/posts/blaugust/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/posts/blaugust/"/></url><url><loc>https://starslayerx.github.io/</loc><lastmod>2023-01-01T08:00:00-07:00</lastmod><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/"/></url><url><loc>https://starslayerx.github.io/archives/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/archives/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/archives/"/></url><url><loc>https://starslayerx.github.io/categories/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/categories/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/categories/"/></url><url><loc>https://starslayerx.github.io/tags/</loc><xhtml:link rel="alternate" hreflang="zh-CN" href="https://starslayerx.github.io/zh-cn/tags/"/><xhtml:link rel="alternate" hreflang="en-US" href="https://starslayerx.github.io/tags/"/></url></urlset>

0 commit comments

Comments
 (0)