Skip to content

Commit 0c75ad1

Browse files
committed
Translate async-await
1 parent 3f65caa commit 0c75ad1

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
- [ ] advanced_foreword (reserved)
3232
- [ ] appcontext [@rosekc](https://github.com/rosekc) rosekc
33-
- [ ] async-await [@rosekc](https://github.com/your_username) rosekc
33+
- [x] async-await [@rosekc](https://github.com/rosekc) rosekc
3434
- [ ] becomingbig
3535
- [x] blueprints [@frostming](https://github.com/frostming) Frost Ming
3636
- [ ] changes

Diff for: docs/locales/zh_CN/LC_MESSAGES/async-await.po

+47-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ msgstr ""
1010
"Report-Msgid-Bugs-To: \n"
1111
"POT-Creation-Date: 2021-05-30 19:27+0000\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13-
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13+
"Last-Translator: rosekc <[email protected]>\n"
1414
"Language-Team: zh_CN <[email protected]>\n"
1515
"MIME-Version: 1.0\n"
1616
"Content-Type: text/plain; charset=utf-8\n"
@@ -19,7 +19,7 @@ msgstr ""
1919

2020
#: ../../async-await.rst:4
2121
msgid "Using ``async`` and ``await``"
22-
msgstr ""
22+
msgstr "使用 ``async`` 和 ``await``"
2323

2424
#: ../../async-await.rst:8
2525
msgid ""
@@ -29,21 +29,27 @@ msgid ""
2929
"where ``contextvars.ContextVar`` is available. This allows views to be "
3030
"defined with ``async def`` and use ``await``."
3131
msgstr ""
32+
"在安装 Flask 时使用 ``async`` 额外后缀(``pip install flask[async]``)后,"
33+
"包括路由、错误处理、请求前、请求后、清理(teardown)的函数都可以使用协程函数。"
34+
"这允许视图函数使用 ``async def`` 定义,以及使用 ``await``。"
3235

3336
#: ../../async-await.rst:21
3437
msgid "Using ``async`` on Windows on Python 3.8"
35-
msgstr ""
38+
msgstr "在 Windows,Python 3.8 环境下使用 ``async``"
3639

3740
#: ../../async-await.rst:23
3841
msgid ""
3942
"Python 3.8 has a bug related to asyncio on Windows. If you encounter "
4043
"something like ``ValueError: set_wakeup_fd only works in main thread``, "
4144
"please upgrade to Python 3.9."
4245
msgstr ""
46+
"在 Windows,Python 3.8 环境下存在一个与 asyncio 相关的 bug。如果遇到"
47+
"类似 ``ValueError: set_wakeup_fd only works in main thread`` 的信息,"
48+
"请更新到 Python 3.9。"
4349

4450
#: ../../async-await.rst:29
4551
msgid "Performance"
46-
msgstr ""
52+
msgstr "性能"
4753

4854
#: ../../async-await.rst:31
4955
msgid ""
@@ -52,6 +58,9 @@ msgid ""
5258
" request comes in to an async view, Flask will start an event loop in a "
5359
"thread, run the view function there, then return the result."
5460
msgstr ""
61+
"异步函数需要一个事件循环来执行。作为一个 WSGI 应用,Flask 使用一个线程去处理"
62+
"请求/响应循环。当请求进入一个异步视图函数时,Flask 将在一个线程中启动一个事件"
63+
"循环,在这个事件循环中执行视图函数,然后返回结果。"
5564

5665
#: ../../async-await.rst:36
5766
msgid ""
@@ -61,6 +70,9 @@ msgid ""
6170
"etc. However, the number of requests your application can handle at one "
6271
"time will remain the same."
6372
msgstr ""
73+
"即使使用异步视图函数,每一个请求依然与一个线程绑定在一起。这样设计的好处是让"
74+
"视图函数中可以执行异步代码,例如多个并行的数据库查询,HTTP请求外部API等等。"
75+
"然而,同一时间应用本身能接受的请求数量不会改变。"
6476

6577
#: ../../async-await.rst:42
6678
msgid ""
@@ -70,10 +82,13 @@ msgid ""
7082
"most use cases, but Flask's async support enables writing and using code "
7183
"that wasn't possible natively before."
7284
msgstr ""
85+
"**异步并不一定比同步代码快。** 异步的优势是在IO密集任务上,但是在CPU密集任务上"
86+
"则不然。传统的 Flask 视图函数在大多数情况下是合适的选择,而 Flask 对异步的支持让"
87+
"运行和使用协程代码成为可能,这是以前原生环境无法做到的。"
7388

7489
#: ../../async-await.rst:50
7590
msgid "Background tasks"
76-
msgstr ""
91+
msgstr "后台任务"
7792

7893
#: ../../async-await.rst:52
7994
msgid ""
@@ -83,6 +98,9 @@ msgid ""
8398
"cancelled. Therefore you cannot spawn background tasks, for example via "
8499
"``asyncio.create_task``."
85100
msgstr ""
101+
"异步函数在其执行完成前,都一个事件循环中运行。当异步函数完成时,事件循环也将停止。"
102+
"这意味着异步函数完成的时候,所有尚未完成的其他衍生任务都将被取消。因此,不能使用"
103+
"类似 ``asyncio.create_task`` 的方法来创建后台任务。"
86104

87105
#: ../../async-await.rst:58
88106
msgid ""
@@ -93,10 +111,14 @@ msgid ""
93111
":ref:`asgi`. This works as the adapter creates an event loop that runs "
94112
"continually."
95113
msgstr ""
114+
"要使用后台任务,最好的方法就是使用任务队列去激活后台任务,而不是在视图函数中"
115+
"创建。考虑到这点,使用 ASGI 服务器来为 Flask 提供服务来创建后台任务,然后如"
116+
" :ref:`asgi` 提到的使用 asgiref 中的 WsgiToAsgi 适配器。这种做法与适配器"
117+
"创建了一个持续运行的事件循环类似。"
96118

97119
#: ../../async-await.rst:67
98120
msgid "When to use Quart instead"
99-
msgstr ""
121+
msgstr "何时使用 Quart 作为替代品"
100122

101123
#: ../../async-await.rst:69
102124
msgid ""
@@ -107,6 +129,10 @@ msgid ""
107129
"handle many concurrent requests, long running requests, and websockets "
108130
"without requiring multiple worker processes or threads."
109131
msgstr ""
132+
"基于实现上的不同,Flask 的异步支持的性能比异步优先的框架会稍低。如果已经拥有"
133+
"一个以异步为主的代码库,考虑使用 `Quart`_ 是明智的选择。Quart 是一个基于 `ASGI`_ "
134+
"的 Flask 重新实现版本(而 Flask 是基于 WSGI 的)。这让多并行请求,长时间运行"
135+
"的请求,以及 websockets 编程不再需要多个工作进程或线程。"
110136

111137
#: ../../async-await.rst:76
112138
msgid ""
@@ -117,10 +143,14 @@ msgid ""
117143
"whether you should use Flask, Quart, or something else is ultimately up "
118144
"to understanding the specific needs of your project."
119145
msgstr ""
146+
"当前已经可以使用 Gevent 或 Eventlet 运行 Flask 来得到使用异步请求处理的好处。"
147+
"这些库通过为底层 Python 库打补丁的方式实现,而 ``async``/``await`` 与 ASGI"
148+
"使用了现代标准 Python 的特性。决定是否应使用 Flask,Quart 或其他工具最终取决于"
149+
"了解项目的特定需求。"
120150

121151
#: ../../async-await.rst:88
122152
msgid "Extensions"
123-
msgstr ""
153+
msgstr "扩展"
124154

125155
#: ../../async-await.rst:90
126156
msgid ""
@@ -131,6 +161,10 @@ msgid ""
131161
"awaitable either and will probably be blocking if called within an async "
132162
"view."
133163
msgstr ""
164+
"Flask 扩展系统的实现先于 Flask 异步支持,所以并不会假设视图函数是异步的。如果扩展"
165+
"提供了对视图函数的有附加功能的装饰器,这些装饰器因为不会异步等待(await)函数运行或者不是"
166+
"异步可等待(awaitable)的,可能在异步视图函数上不能正常运行。扩展提供的其他函数或许"
167+
"同样不是异步可等待的,在异步视图函数调用的时候大概会阻塞。"
134168

135169
#: ../../async-await.rst:96
136170
msgid ""
@@ -139,23 +173,28 @@ msgid ""
139173
"provides a view function decorator add ``ensure_sync`` before calling the"
140174
" decorated function,"
141175
msgstr ""
176+
"扩展作者可以通过使用 :meth:`flask.Flask.ensure_sync` 方法来支持异步函数。"
177+
"举例来说,如果扩展提供了一个视图函数装饰器,使用 ``ensure_sync`` 调用被包裹的函数。"
142178

143179
#: ../../async-await.rst:111
144180
msgid ""
145181
"Check the changelog of the extension you want to use to see if they've "
146182
"implemented async support, or make a feature request or PR to them."
147183
msgstr ""
184+
"检查扩展的更新记录,查看是否实现了异步支持。如果没有可以向他们提交 PR。"
148185

149186
#: ../../async-await.rst:116
150187
msgid "Other event loops"
151-
msgstr ""
188+
msgstr "其他事件循环"
152189

153190
#: ../../async-await.rst:118
154191
msgid ""
155192
"At the moment Flask only supports :mod:`asyncio`. It's possible to "
156193
"override :meth:`flask.Flask.ensure_sync` to change how async functions "
157194
"are wrapped to use a different library."
158195
msgstr ""
196+
"当前 Flask 只支持 :mod:`asyncio`。覆盖 :meth:`flask.Flask.ensure_sync` "
197+
"可以修改包裹异步函数的实现,以使用其他库。"
159198

160199
#~ msgid ""
161200
#~ "Routes, error handlers, before request, "
@@ -166,4 +205,3 @@ msgstr ""
166205
#~ " allows views to be defined with "
167206
#~ "``async def`` and use ``await``."
168207
#~ msgstr ""
169-

0 commit comments

Comments
 (0)