|
@@ -7,8 +7,8 @@ msgid ""
|
|
|
msgstr ""
|
|
|
"Project-Id-Version: PyWebIO 1.1.0\n"
|
|
|
"Report-Msgid-Bugs-To: \n"
|
|
|
-"POT-Creation-Date: 2021-03-14 16:01+0800\n"
|
|
|
-"PO-Revision-Date: 2021-03-14 16:05+0800\n"
|
|
|
+"POT-Creation-Date: 2021-03-14 21:29+0800\n"
|
|
|
+"PO-Revision-Date: 2021-03-14 21:31+0800\n"
|
|
|
"Last-Translator: WangWeimin <wang0.618@qq.com>\n"
|
|
|
"Language: zh_CN\n"
|
|
|
"Language-Team: \n"
|
|
@@ -915,8 +915,10 @@ msgid ""
|
|
|
"In PyWebIO, there are two modes to run PyWebIO applications: running as a script and using `start_server() <pywebio.platform.tornado.start_server>` "
|
|
|
"or `path_deploy() <pywebio.platform.path_deploy>` to run as a web service."
|
|
|
msgstr ""
|
|
|
+"在PyWebIO中,有两种方式用来运行PyWebIO应用:作为脚本运行和使用 `start_server() <pywebio.platform.tornado.start_server>` 或 `path_deploy() <pywebio."
|
|
|
+"platform.path_deploy>` 来作为Web服务运行。"
|
|
|
|
|
|
-#: ../../guide.rst:595 ../../guide.rst:664
|
|
|
+#: ../../guide.rst:595 ../../guide.rst:666
|
|
|
msgid "**Server mode**"
|
|
|
msgstr "**Server模式**"
|
|
|
|
|
@@ -976,9 +978,11 @@ msgstr ""
|
|
|
|
|
|
#: ../../guide.rst:618
|
|
|
msgid ""
|
|
|
-"Use `path_deploy() <pywebio.platform.path_deploy>` to deploy the PyWebIO applications from a directory. A valid python file under this directory "
|
|
|
-"need contain the ``main`` function as the PyWebIO application. You can access the application by using the file path as the URL."
|
|
|
+"Use `path_deploy() <pywebio.platform.path_deploy>` to deploy the PyWebIO applications from a directory. The python file under this directory need "
|
|
|
+"contain the ``main`` function to be seen as the PyWebIO application. You can access the application by using the file path as the URL."
|
|
|
msgstr ""
|
|
|
+"使用 `path_deploy() <pywebio.platform.path_deploy>` 可以从一个路径中部署PyWebIO应用。位于该路径下的python文件需要包含名字为 ``main`` 的PyWebIO任务函"
|
|
|
+"数才能被视为PyWebIO应用程序。服务端会根据用户访问的URL来确定需要加载的文件并从中读取PyWebIO应用来运行。"
|
|
|
|
|
|
#: ../../guide.rst:622
|
|
|
msgid "For example, given the following folder structure::"
|
|
@@ -1006,6 +1010,14 @@ msgstr ""
|
|
|
|
|
|
#: ../../guide.rst:634
|
|
|
msgid ""
|
|
|
+"You can also use the command ``pywebio-path-deploy`` to start a server just like using `path_deploy() <pywebio.platform.path_deploy>`. For more "
|
|
|
+"information, refer ``pywebio-path-deploy --help``"
|
|
|
+msgstr ""
|
|
|
+"你还可以使用 ``pywebio-path-deploy`` 命令来启动一个和 `path_deploy() <pywebio.platform.path_deploy>` 效果一样的server。关于命令的更多信息请查阅命令"
|
|
|
+"帮助: ``pywebio-path-deploy —help``"
|
|
|
+
|
|
|
+#: ../../guide.rst:636
|
|
|
+msgid ""
|
|
|
"In Server mode, you can use `pywebio.platform.seo()` to set the `SEO <https://en.wikipedia.org/wiki/Search_engine_optimization>`_ information. If "
|
|
|
"not ``seo()`` is not used, the `docstring <https://www.python.org/dev/peps/pep-0257/>`_ of the task function will be regarded as SEO information by "
|
|
|
"default."
|
|
@@ -1013,13 +1025,13 @@ msgstr ""
|
|
|
"在Server模式下,可以使用 `pywebio.platform.seo()` 函数来设置任务函数SEO信息(在被搜索引擎索引时提供的网页信息,包含应用标题和应用简介),如果不使用 "
|
|
|
"``seo()`` 函数,默认条件下,PyWebIO会将任务函数的函数注释作为SEO信息(应用标题和简介之间使用一个空行分隔)。"
|
|
|
|
|
|
-#: ../../guide.rst:638
|
|
|
+#: ../../guide.rst:640
|
|
|
msgid ""
|
|
|
"Note that in Server mode, PyWebIO's input and output functions can only be called in the context of task functions. For example, the following code "
|
|
|
"is **not allowed**::"
|
|
|
msgstr "注意,在Server模式下,仅能在任务函数上下文中对PyWebIO的交互函数进行调用。比如如下调用是 **不被允许的** ::"
|
|
|
|
|
|
-#: ../../guide.rst:640
|
|
|
+#: ../../guide.rst:642
|
|
|
msgid ""
|
|
|
"import pywebio\n"
|
|
|
"from pywebio.input import input\n"
|
|
@@ -1028,15 +1040,15 @@ msgid ""
|
|
|
"pywebio.start_server(my_task_func, port=int(port))"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:647 ../../guide.rst:660
|
|
|
+#: ../../guide.rst:649 ../../guide.rst:662
|
|
|
msgid "**Script mode**"
|
|
|
msgstr "**Script模式**"
|
|
|
|
|
|
-#: ../../guide.rst:649
|
|
|
+#: ../../guide.rst:651
|
|
|
msgid "In Script mode, PyWebIO input and output functions can be called anywhere."
|
|
|
msgstr "Script模式下,在任何位置都可以调用PyWebIO的交互函数。"
|
|
|
|
|
|
-#: ../../guide.rst:651
|
|
|
+#: ../../guide.rst:653
|
|
|
msgid ""
|
|
|
"If the user closes the browser before the end of the session, then calls to PyWebIO input and output functions in the session will cause a "
|
|
|
"`SessionException <pywebio.exceptions.SessionException>` exception."
|
|
@@ -1044,15 +1056,15 @@ msgstr ""
|
|
|
"如果用户在会话结束之前关闭了浏览器,那么之后会话内对于PyWebIO交互函数的调用将会引发一个 `SessionException <pywebio.exceptions.SessionException>` 异"
|
|
|
"常。"
|
|
|
|
|
|
-#: ../../guide.rst:656
|
|
|
+#: ../../guide.rst:658
|
|
|
msgid "Concurrent"
|
|
|
msgstr "并发"
|
|
|
|
|
|
-#: ../../guide.rst:658
|
|
|
+#: ../../guide.rst:660
|
|
|
msgid "PyWebIO can be used in a multi-threading environment."
|
|
|
msgstr "PyWebIO 支持在多线程环境中使用。"
|
|
|
|
|
|
-#: ../../guide.rst:662
|
|
|
+#: ../../guide.rst:664
|
|
|
msgid ""
|
|
|
"In Script mode, you can freely start new thread and call PyWebIO interactive functions in it. When all `non-daemonic <https://docs.python.org/3/"
|
|
|
"library/threading.html#thread-objects>`_ threads finish running, the script exits."
|
|
@@ -1060,7 +1072,7 @@ msgstr ""
|
|
|
"在 Script模式下,你可以自由地启动线程,并在其中调用PyWebIO的交互函数。当所有非 `Daemon线程 <https://docs.python.org/3/library/threading.html#thread-"
|
|
|
"objects>`_ 运行结束后,脚本退出。"
|
|
|
|
|
|
-#: ../../guide.rst:666
|
|
|
+#: ../../guide.rst:668
|
|
|
msgid ""
|
|
|
"In Server mode, if you need to use PyWebIO interactive functions in new thread, you need to use `register_thread(thread) <pywebio.session."
|
|
|
"register_thread>` to register the new thread (so that PyWebIO can know which session the thread belongs to). If the PyWebIO interactive function is "
|
|
@@ -1075,11 +1087,11 @@ msgstr ""
|
|
|
"理,其调用PyWebIO的交互函数将会产生 `SessionNotFoundException <pywebio.exceptions.SessionNotFoundException>` 异常。\n"
|
|
|
"当会话的任务函数和会话内通过 `register_thread(thread) <pywebio.session.register_thread>` 注册的线程都结束运行时,会话关闭。"
|
|
|
|
|
|
-#: ../../guide.rst:668
|
|
|
+#: ../../guide.rst:670
|
|
|
msgid "Example of using multi-threading in Server mode::"
|
|
|
msgstr "Server模式下多线程的使用示例::"
|
|
|
|
|
|
-#: ../../guide.rst:670
|
|
|
+#: ../../guide.rst:672
|
|
|
msgid ""
|
|
|
"def show_time():\n"
|
|
|
" while True:\n"
|
|
@@ -1102,11 +1114,11 @@ msgid ""
|
|
|
"start_server(app, port=8080, debug=True)"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:694 ../../guide.rst:930
|
|
|
+#: ../../guide.rst:696 ../../guide.rst:932
|
|
|
msgid "Close of session"
|
|
|
msgstr "会话的结束"
|
|
|
|
|
|
-#: ../../guide.rst:696
|
|
|
+#: ../../guide.rst:698
|
|
|
msgid ""
|
|
|
"The close of session may also be caused by the user closing the browser page. After the browser page is closed, PyWebIO input function calls that "
|
|
|
"have not yet returned in the current session will cause `SessionClosedException <pywebio.exceptions.SessionClosedException>`, and subsequent calls "
|
|
@@ -1117,7 +1129,7 @@ msgstr ""
|
|
|
"SessionClosedException>` 异常,之后对于PyWebIO交互函数的调用将会产生 `SessionNotFoundException <pywebio.exceptions.SessionNotFoundException>` 或 "
|
|
|
"`SessionClosedException <pywebio.exceptions.SessionClosedException>` 异常。"
|
|
|
|
|
|
-#: ../../guide.rst:698
|
|
|
+#: ../../guide.rst:700
|
|
|
msgid ""
|
|
|
"You can use `defer_call(func) <pywebio.session.defer_call>` to set the function to be called when the session closes. Whether it is because the "
|
|
|
"user closes the page or the task finishes to cause session closed, the function set by `defer_call(func) <pywebio.session.defer_call>` will be "
|
|
@@ -1129,33 +1141,33 @@ msgstr ""
|
|
|
"`defer_call(func) <pywebio.session.defer_call>` 可以用于资源清理等工作。在会话中可以多次调用 `defer_call() <pywebio.session.defer_call>` ,会话结束后"
|
|
|
"将会顺序执行设置的函数。"
|
|
|
|
|
|
-#: ../../guide.rst:703
|
|
|
+#: ../../guide.rst:705
|
|
|
msgid "Integration with web framework"
|
|
|
msgstr "与Web框架集成"
|
|
|
|
|
|
-#: ../../guide.rst:705
|
|
|
+#: ../../guide.rst:707
|
|
|
msgid ""
|
|
|
"The PyWebIO application can be integrated into an existing Python Web project, and the PyWebIO application and the Web project share a web "
|
|
|
"framework. PyWebIO currently supports integration with Flask, Tornado, Django and aiohttp web frameworks."
|
|
|
msgstr "可以将PyWebIO应用集成到现有的Python Web项目中,PyWebIO应用与Web项目共用一个Web框架。目前支持与Flask、Tornado、Django和aiohttp Web框架的集成。"
|
|
|
|
|
|
-#: ../../guide.rst:707
|
|
|
+#: ../../guide.rst:709
|
|
|
msgid "The integration methods of different web frameworks are as follows:"
|
|
|
msgstr "不同Web框架的集成方法如下:"
|
|
|
|
|
|
-#: ../../guide.rst:711
|
|
|
+#: ../../guide.rst:713
|
|
|
msgid "Tornado"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:715
|
|
|
+#: ../../guide.rst:717
|
|
|
msgid "**Tornado**"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:717
|
|
|
+#: ../../guide.rst:719
|
|
|
msgid "Need to add a ``RequestHandler`` to Tornado application::"
|
|
|
msgstr "需要在Tornado应用中引入一个 ``RequestHandler`` ::"
|
|
|
|
|
|
-#: ../../guide.rst:719
|
|
|
+#: ../../guide.rst:721
|
|
|
msgid ""
|
|
|
"import tornado.ioloop\n"
|
|
|
"import tornado.web\n"
|
|
@@ -1175,7 +1187,7 @@ msgid ""
|
|
|
" tornado.ioloop.IOLoop.current().start()"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:737
|
|
|
+#: ../../guide.rst:739
|
|
|
msgid ""
|
|
|
"In above code, we use `webio_handler(task_func) <pywebio.platform.tornado.webio_handler>` to get the Tornado `WebSocketHandler <https://www."
|
|
|
"tornadoweb.org/en/stable/websocket.html#tornado.websocket.WebSocketHandler>`_ that communicates with the browser, and bind it to the ``/tool`` "
|
|
@@ -1185,7 +1197,7 @@ msgstr ""
|
|
|
"www.tornadoweb.org/en/stable/websocket.html#tornado.websocket.WebSocketHandler>`_ ,并将其绑定在 ``/tool`` 路由下。启动Tornado服务器后,访问 "
|
|
|
"``http://localhost/tool`` 即可打开PyWebIO应用。"
|
|
|
|
|
|
-#: ../../guide.rst:741
|
|
|
+#: ../../guide.rst:743
|
|
|
msgid ""
|
|
|
"PyWebIO uses the WebSocket protocol to communicate with the browser in Tornado. If your Tornado application is behind a reverse proxy (such as "
|
|
|
"Nginx), you may need to configure the reverse proxy to support the WebSocket protocol. :ref:`Here <nginx_ws_config>` is an example of Nginx "
|
|
@@ -1194,19 +1206,19 @@ msgstr ""
|
|
|
"当使用Tornado后端时,PyWebIO使用WebSocket协议和浏览器进行通讯,如果你的Tornado应用处在反向代理(比如Nginx)之后,可能需要特别配置反向代理来支持"
|
|
|
"WebSocket协议,:ref:`这里 <nginx_ws_config>` 有一个Nginx配置WebSocket的例子。"
|
|
|
|
|
|
-#: ../../guide.rst:743
|
|
|
+#: ../../guide.rst:745
|
|
|
msgid "Flask"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:747
|
|
|
+#: ../../guide.rst:749
|
|
|
msgid "**Flask**"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:749
|
|
|
+#: ../../guide.rst:751
|
|
|
msgid "One route need to be added to communicate with the browser through HTTP::"
|
|
|
msgstr "需要添加一个PyWebIO相关的路由,用来和浏览器进行Http通讯::"
|
|
|
|
|
|
-#: ../../guide.rst:751
|
|
|
+#: ../../guide.rst:753
|
|
|
msgid ""
|
|
|
"from pywebio.platform.flask import webio_view\n"
|
|
|
"from pywebio import STATIC_PATH\n"
|
|
@@ -1221,7 +1233,7 @@ msgid ""
|
|
|
"app.run(host='localhost', port=80)"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:764
|
|
|
+#: ../../guide.rst:766
|
|
|
msgid ""
|
|
|
"In above code, we use `webio_view(task_func) <pywebio.platform.flask.webio_view>` to get the Flask view of the PyWebIO application, and bind it to "
|
|
|
"``/tool`` path. After starting the Flask application, visit ``http://localhost/tool`` to open the PyWebIO application."
|
|
@@ -1230,19 +1242,19 @@ msgstr ""
|
|
|
"flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.add_url_rule>`_ 将其绑定在 ``/tool`` 路径下。启动Flask应用后,访问 ``http://localhost/tool`` 即"
|
|
|
"可打开PyWebIO应用。"
|
|
|
|
|
|
-#: ../../guide.rst:766
|
|
|
+#: ../../guide.rst:768
|
|
|
msgid "Django"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:770
|
|
|
+#: ../../guide.rst:772
|
|
|
msgid "**Django**"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:772
|
|
|
+#: ../../guide.rst:774
|
|
|
msgid "Need to add a route in ``urls.py``::"
|
|
|
msgstr "在django的路由配置文件 ``urls.py`` 中加入PyWebIO相关的路由即可::"
|
|
|
|
|
|
-#: ../../guide.rst:774
|
|
|
+#: ../../guide.rst:776
|
|
|
msgid ""
|
|
|
"# urls.py\n"
|
|
|
"\n"
|
|
@@ -1260,7 +1272,7 @@ msgid ""
|
|
|
"]"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:790
|
|
|
+#: ../../guide.rst:792
|
|
|
msgid ""
|
|
|
"In above code, we add a routing rule to bind the view function of the PyWebIO application to the ``/tool`` path After starting the Django server, "
|
|
|
"visit ``http://localhost/tool`` to open the PyWebIO application"
|
|
@@ -1268,19 +1280,19 @@ msgstr ""
|
|
|
"以上代码使用添加了一条路由规则将PyWebIO应用的视图函数绑定到 ``/tool`` 路径下。\n"
|
|
|
"启动Django应用后,访问 ``http://localhost/tool`` 即可打开PyWebIO应用"
|
|
|
|
|
|
-#: ../../guide.rst:793
|
|
|
+#: ../../guide.rst:795
|
|
|
msgid "aiohttp"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:797
|
|
|
+#: ../../guide.rst:799
|
|
|
msgid "**aiohttp**"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:799
|
|
|
+#: ../../guide.rst:801
|
|
|
msgid "One route need to be added to communicate with the browser through WebSocket:::"
|
|
|
msgstr "需要添加一个PyWebIO相关的路由,用来和浏览器进行WebSocket通讯::"
|
|
|
|
|
|
-#: ../../guide.rst:801
|
|
|
+#: ../../guide.rst:803
|
|
|
msgid ""
|
|
|
"from aiohttp import web\n"
|
|
|
"from pywebio.platform.aiohttp import static_routes, webio_handler\n"
|
|
@@ -1292,11 +1304,11 @@ msgid ""
|
|
|
"web.run_app(app, host='localhost', port=80)"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:810
|
|
|
+#: ../../guide.rst:812
|
|
|
msgid "After starting the aiohttp server, visit ``http://localhost/tool`` to open the PyWebIO application"
|
|
|
msgstr "启动aiohttp应用后,访问 ``http://localhost/tool`` 即可打开PyWebIO应用"
|
|
|
|
|
|
-#: ../../guide.rst:814
|
|
|
+#: ../../guide.rst:816
|
|
|
msgid ""
|
|
|
"PyWebIO uses the WebSocket protocol to communicate with the browser in aiohttp. If your aiohttp server is behind a reverse proxy (such as Nginx), "
|
|
|
"you may need to configure the reverse proxy to support the WebSocket protocol. :ref:`Here <nginx_ws_config>` is an example of Nginx WebSocket "
|
|
@@ -1305,15 +1317,15 @@ msgstr ""
|
|
|
"当使用aiohttp后端时,PyWebIO使用WebSocket协议和浏览器进行通讯,如果你的aiohttp应用处在反向代理(比如Nginx)之后,\n"
|
|
|
"可能需要特别配置反向代理来支持WebSocket协议,:ref:`这里 <nginx_ws_config>` 有一个Nginx配置WebSocket的例子。"
|
|
|
|
|
|
-#: ../../guide.rst:819
|
|
|
+#: ../../guide.rst:821
|
|
|
msgid "Notes"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:820
|
|
|
+#: ../../guide.rst:822
|
|
|
msgid "**Static resources Hosting**"
|
|
|
msgstr "**PyWebIO静态资源的托管**"
|
|
|
|
|
|
-#: ../../guide.rst:822
|
|
|
+#: ../../guide.rst:824
|
|
|
msgid ""
|
|
|
"By default, the front-end of PyWebIO gets required static resources from CDN. If you want to deploy PyWebIO applications in an offline environment, "
|
|
|
"you need to host static files by yourself, and set the ``cdn`` parameter of ``webio_view()`` or ``webio_handler()`` to ``False``."
|
|
@@ -1321,7 +1333,7 @@ msgstr ""
|
|
|
"PyWebIO默认使用CDN来获取前端的静态资源,如果要将PyWebIO应用部署到离线环境中,需要自行托管静态文件,\n"
|
|
|
"并将 ``webio_view()`` 或 ``webio_handler()`` 的 ``cdn`` 参数设置为 ``False`` 。"
|
|
|
|
|
|
-#: ../../guide.rst:824
|
|
|
+#: ../../guide.rst:826
|
|
|
msgid ""
|
|
|
"When setting ``cdn=False`` , you need to host the static resources in the same directory as the PyWebIO application. In addition, you can also pass "
|
|
|
"a string to ``cdn`` parameter to directly set the deployment directory of PyWebIO static resources."
|
|
@@ -1329,30 +1341,30 @@ msgstr ""
|
|
|
"``cdn=False`` 时需要将静态资源托管在和PyWebIO应用同级的目录下。\n"
|
|
|
"同时,也可以通过 ``cdn`` 参数直接设置PyWebIO静态资源的部署目录。"
|
|
|
|
|
|
-#: ../../guide.rst:827
|
|
|
+#: ../../guide.rst:829
|
|
|
msgid ""
|
|
|
"The path of the static file of PyWebIO is stored in ``pywebio.STATIC_PATH``, you can use the command ``python3 -c \"import pywebio; print(pywebio."
|
|
|
"STATIC_PATH)\"`` to print it out."
|
|
|
msgstr ""
|
|
|
"PyWebIO的静态文件的路径保存在 ``pywebio.STATIC_PATH`` 中,可使用命令 ``python3 -c \"import pywebio; print(pywebio.STATIC_PATH)\"`` 将其打印出来。"
|
|
|
|
|
|
-#: ../../guide.rst:829
|
|
|
+#: ../../guide.rst:831
|
|
|
msgid ""
|
|
|
"``start_server()`` also support ``cdn`` parameter, if it is set to ``False``, the static resource will be hosted in local server automatically, "
|
|
|
"without manual hosting."
|
|
|
msgstr "使用 ``start_server()`` 启动的应用,如果将 ``cdn`` 参数设置为 ``False`` ,会自动启动一个本地的静态资源托管服务,无需手动托管。"
|
|
|
|
|
|
-#: ../../guide.rst:835
|
|
|
+#: ../../guide.rst:837
|
|
|
msgid "Coroutine-based session"
|
|
|
msgstr "基于协程的会话"
|
|
|
|
|
|
-#: ../../guide.rst:837
|
|
|
+#: ../../guide.rst:839
|
|
|
msgid ""
|
|
|
"This section will introduce the advanced features of PyWebIO --- coroutine-based session. In most cases, you don’t need it. All functions or "
|
|
|
"methods in PyWebIO that are only used for coroutine sessions are specifically noted in the document."
|
|
|
msgstr "关于协程内容属于高级特性,您不必使用此部分也可以实现PyWebIO支持的全部功能。PyWebIO中所有仅用于协程会话的函数或方法都在文档中有特别说明。"
|
|
|
|
|
|
-#: ../../guide.rst:839
|
|
|
+#: ../../guide.rst:841
|
|
|
msgid ""
|
|
|
"PyWebIO's session is based on thread by default. Each time a user opens a session connection to the server, PyWebIO will start a thread to run the "
|
|
|
"task function. In addition to thread-based sessions, PyWebIO also provides coroutine-based sessions. Coroutine-based sessions accept coroutine "
|
|
@@ -1361,7 +1373,7 @@ msgstr ""
|
|
|
"PyWebIO的会话实现默认是基于线程的,用户每打开一个和服务端的会话连接,PyWebIO会启动一个线程来运行任务函数。\n"
|
|
|
"除了基于线程的会话,PyWebIO还提供了基于协程的会话。基于协程的会话接受协程函数作为任务函数。"
|
|
|
|
|
|
-#: ../../guide.rst:841
|
|
|
+#: ../../guide.rst:843
|
|
|
msgid ""
|
|
|
"The session based on the coroutine is a single-thread model, which means that all sessions run in a single thread. For IO-bound tasks, coroutines "
|
|
|
"take up fewer resources than threads and have performance comparable to threads. In addition, the context switching of the coroutine is "
|
|
@@ -1370,17 +1382,17 @@ msgstr ""
|
|
|
"基于协程的会话为单线程模型,所有会话都运行在一个线程内。对于IO密集型的任务,协程比线程占用更少的资源同时又拥有媲美于线程的性能。\n"
|
|
|
"另外,协程的上下文切换具有可预测性,能够减少程序同步与加锁的需要,可以有效避免大多数临界区问题。"
|
|
|
|
|
|
-#: ../../guide.rst:844
|
|
|
+#: ../../guide.rst:846
|
|
|
msgid "Using coroutine session"
|
|
|
msgstr "使用协程会话"
|
|
|
|
|
|
-#: ../../guide.rst:846
|
|
|
+#: ../../guide.rst:848
|
|
|
msgid ""
|
|
|
"To use coroutine-based session, you need to use the ``async`` keyword to declare the task function as a coroutine function, and use the ``await`` "
|
|
|
"syntax to call the PyWebIO input function:"
|
|
|
msgstr "要使用基于协程的会话,需要使用 ``async`` 关键字将任务函数声明为协程函数,并使用 ``await`` 语法调用PyWebIO输入函数:"
|
|
|
|
|
|
-#: ../../guide.rst:848
|
|
|
+#: ../../guide.rst:850
|
|
|
#, python-format
|
|
|
msgid ""
|
|
|
" from pywebio.input import *\n"
|
|
@@ -1394,7 +1406,7 @@ msgid ""
|
|
|
" start_server(say_hello, auto_open_webbrowser=True)"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:862
|
|
|
+#: ../../guide.rst:864
|
|
|
msgid ""
|
|
|
"In the coroutine task function, you can also use ``await`` to call other coroutines or ( `awaitable objects <https://docs.python.org/3/library/"
|
|
|
"asyncio-task.html#asyncio-awaitables>`_ ) in the standard library `asyncio <https://docs.python.org/3/library/asyncio.html>`_:"
|
|
@@ -1402,7 +1414,7 @@ msgstr ""
|
|
|
"在协程任务函数中,也可以使用 ``await`` 调用其他协程或标准库 `asyncio <https://docs.python.org/3/library/asyncio.html>`_ 中的可等待对象( `awaitable "
|
|
|
"objects <https://docs.python.org/3/library/asyncio-task.html#asyncio-awaitables>`_ ):"
|
|
|
|
|
|
-#: ../../guide.rst:864
|
|
|
+#: ../../guide.rst:866
|
|
|
msgid ""
|
|
|
" import asyncio\n"
|
|
|
" from pywebio import start_server\n"
|
|
@@ -1419,7 +1431,7 @@ msgid ""
|
|
|
" start_server(main, auto_open_webbrowser=True)"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:883
|
|
|
+#: ../../guide.rst:885
|
|
|
msgid ""
|
|
|
"In coroutine-based session, all input functions defined in the :doc:`pywebio.input </input>` module need to use ``await`` syntax to get the return "
|
|
|
"value. Forgetting to use ``await`` will be a common error when using coroutine-based session."
|
|
@@ -1427,47 +1439,47 @@ msgstr ""
|
|
|
"在基于协程的会话中, :doc:`pywebio.input </input>` 模块中的定义输入函数都需要使用 ``await`` 语法来获取返回值,忘记使用 ``await`` 将会是在使用基于协"
|
|
|
"程的会话时常出现的错误。"
|
|
|
|
|
|
-#: ../../guide.rst:885
|
|
|
+#: ../../guide.rst:887
|
|
|
msgid "Other functions that need to use ``await`` syntax in the coroutine session are:"
|
|
|
msgstr "其他在协程会话中也需要使用 ``await`` 语法来进行调用函数有:"
|
|
|
|
|
|
-#: ../../guide.rst:887
|
|
|
+#: ../../guide.rst:889
|
|
|
msgid "`pywebio.session.run_asyncio_coroutine(coro_obj) <pywebio.session.run_asyncio_coroutine>`"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:888
|
|
|
+#: ../../guide.rst:890
|
|
|
msgid "`pywebio.session.eval_js(expression) <pywebio.session.eval_js>`"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:889
|
|
|
+#: ../../guide.rst:891
|
|
|
msgid "`pywebio.session.hold() <pywebio.session.hold>`"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:893
|
|
|
+#: ../../guide.rst:895
|
|
|
msgid ""
|
|
|
"Although the PyWebIO coroutine session is compatible with the ``awaitable objects`` in the standard library ``asyncio``, the ``asyncio`` library is "
|
|
|
"not compatible with the ``awaitable objects`` in the PyWebIO coroutine session."
|
|
|
msgstr "虽然PyWebIO的协程会话兼容标准库 ``asyncio`` 中的 ``awaitable objects`` ,但 ``asyncio`` 库不兼容PyWebIO协程会话中的 ``awaitable objects`` ."
|
|
|
|
|
|
-#: ../../guide.rst:895
|
|
|
+#: ../../guide.rst:897
|
|
|
msgid ""
|
|
|
"That is to say, you can't pass PyWebIO ``awaitable objects`` to the `asyncio`` functions that accept ``awaitable objects``. For example, the "
|
|
|
"following calls are **not supported** ::"
|
|
|
msgstr ""
|
|
|
"也就是说,无法将PyWebIO中的 ``awaitable objects`` 传入 ``asyncio`` 中的接受 ``awaitable objects`` 作为参数的函数中,比如如下调用是 **不被支持的** ::"
|
|
|
|
|
|
-#: ../../guide.rst:897
|
|
|
+#: ../../guide.rst:899
|
|
|
msgid ""
|
|
|
"await asyncio.shield(pywebio.input())\n"
|
|
|
"await asyncio.gather(asyncio.sleep(1), pywebio.session.eval_js('1+1'))\n"
|
|
|
"task = asyncio.create_task(pywebio.input())"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:904
|
|
|
+#: ../../guide.rst:906
|
|
|
msgid "Concurrency in coroutine-based sessions"
|
|
|
msgstr "协程会话的并发"
|
|
|
|
|
|
-#: ../../guide.rst:906
|
|
|
+#: ../../guide.rst:908
|
|
|
msgid ""
|
|
|
"In coroutine-based session, you can start new thread, but you cannot call PyWebIO interactive functions in it (`register_thread() <pywebio.session."
|
|
|
"register_thread>` is not available in coroutine session). But you can use `run_async(coro) <pywebio.session.run_async>` to execute a coroutine "
|
|
@@ -1477,7 +1489,7 @@ msgstr ""
|
|
|
"用)。\n"
|
|
|
"但你可以使用 `run_async(coro) <pywebio.session.run_async>` 来异步执行一个协程对象,新协程内可以使用PyWebIO交互函数:"
|
|
|
|
|
|
-#: ../../guide.rst:908
|
|
|
+#: ../../guide.rst:910
|
|
|
msgid ""
|
|
|
" from pywebio import start_server\n"
|
|
|
" from pywebio.session import run_async\n"
|
|
@@ -1495,7 +1507,7 @@ msgid ""
|
|
|
" start_server(main, auto_open_webbrowser=True)"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:927
|
|
|
+#: ../../guide.rst:929
|
|
|
msgid ""
|
|
|
"`run_async(coro) <pywebio.session.run_async>` returns a `TaskHandler <pywebio.session.coroutinebased.TaskHandler>`, which can be used to query the "
|
|
|
"running status of the coroutine or close the coroutine."
|
|
@@ -1503,13 +1515,13 @@ msgstr ""
|
|
|
"`run_async(coro) <pywebio.session.run_async>` 返回一个 `TaskHandler <pywebio.session.coroutinebased.TaskHandler>` ,通过该 `TaskHandler <pywebio."
|
|
|
"session.coroutinebased.TaskHandler>` 可以查询协程运行状态和关闭协程。"
|
|
|
|
|
|
-#: ../../guide.rst:932
|
|
|
+#: ../../guide.rst:934
|
|
|
msgid ""
|
|
|
"Similar to thread-based session, in coroutine-based session, when the task function and the coroutine running through `run_async() <pywebio.session."
|
|
|
"run_async>` in the session are all finished, the session is closed."
|
|
|
msgstr "与基于线程的会话类似,在基于协程的会话中,当任务函数和在会话内通过 `run_async() <pywebio.session.run_async>` 运行的协程全部结束后,会话关闭。"
|
|
|
|
|
|
-#: ../../guide.rst:934
|
|
|
+#: ../../guide.rst:936
|
|
|
msgid ""
|
|
|
"If the close of the session is caused by the user closing the browser, the behavior of PyWebIO is the same as :ref:`Thread-based session "
|
|
|
"<session_close>`: After the browser page closed, PyWebIO input function calls that have not yet returned in the current session will cause "
|
|
@@ -1521,23 +1533,23 @@ msgstr ""
|
|
|
"的调用将会产生 `SessionNotFoundException <pywebio.exceptions.SessionNotFoundException>` 或 `SessionClosedException <pywebio.exceptions."
|
|
|
"SessionClosedException>` 异常。"
|
|
|
|
|
|
-#: ../../guide.rst:936
|
|
|
+#: ../../guide.rst:938
|
|
|
msgid "`defer_call(func) <pywebio.session.defer_call>` also available in coroutine session."
|
|
|
msgstr "协程会话也同样支持使用 `defer_call(func) <pywebio.session.defer_call>` 来设置会话结束时需要调用的函数。"
|
|
|
|
|
|
-#: ../../guide.rst:941
|
|
|
+#: ../../guide.rst:943
|
|
|
msgid "Integration with Web Framework"
|
|
|
msgstr "协程会话与Web框架集成"
|
|
|
|
|
|
-#: ../../guide.rst:943
|
|
|
+#: ../../guide.rst:945
|
|
|
msgid "The PyWebIO application that using coroutine-based session can also be integrated to the web framework."
|
|
|
msgstr "基于协程的会话同样可以与Web框架进行集成,只需要在原来传入任务函数的地方改为传入协程函数即可。"
|
|
|
|
|
|
-#: ../../guide.rst:945
|
|
|
+#: ../../guide.rst:947
|
|
|
msgid "However, there are some limitations when using coroutine-based sessions to integrate into Flask or Django:"
|
|
|
msgstr "但当前在使用基于协程的会话集成进Flask或Django时,存在一些限制:"
|
|
|
|
|
|
-#: ../../guide.rst:947
|
|
|
+#: ../../guide.rst:949
|
|
|
msgid ""
|
|
|
"First, when ``await`` the coroutine objects/awaitable objects in the ``asyncio`` module, you need to use `run_asyncio_coroutine() <pywebio.session."
|
|
|
"run_asyncio_coroutine>` to wrap the coroutine object."
|
|
@@ -1545,15 +1557,15 @@ msgstr ""
|
|
|
"一是协程函数内还无法直接通过 ``await`` 直接等待asyncio库中的协程对象,目前需要使用 `run_asyncio_coroutine() <pywebio.session."
|
|
|
"run_asyncio_coroutine>` 进行包装。"
|
|
|
|
|
|
-#: ../../guide.rst:949
|
|
|
+#: ../../guide.rst:951
|
|
|
msgid "Secondly, you need to start a new thread to run the event loop before starting a Flask/Django server."
|
|
|
msgstr "二是,在启动Flask/Django这类基于线程的服务器之前需要启动一个单独的线程来运行事件循环。"
|
|
|
|
|
|
-#: ../../guide.rst:951
|
|
|
+#: ../../guide.rst:953
|
|
|
msgid "Example of coroutine-based session integration into Flask:"
|
|
|
msgstr "使用基于协程的会话集成进Flask的示例:"
|
|
|
|
|
|
-#: ../../guide.rst:953
|
|
|
+#: ../../guide.rst:955
|
|
|
msgid ""
|
|
|
" import asyncio\n"
|
|
|
" import threading\n"
|
|
@@ -1578,21 +1590,21 @@ msgid ""
|
|
|
" app.run(host='localhost', port=80)"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:978
|
|
|
+#: ../../guide.rst:980
|
|
|
msgid ""
|
|
|
"Finally, coroutine-based session is not available in the Script mode. You always need to use ``start_server()`` to run coroutine task function or "
|
|
|
"integrate it to a web framework."
|
|
|
msgstr "最后,使用PyWebIO编写的协程函数不支持Script模式,总是需要使用 ``start_server`` 来启动一个服务或者集成进Web框架来调用。"
|
|
|
|
|
|
-#: ../../guide.rst:981
|
|
|
+#: ../../guide.rst:983
|
|
|
msgid "Last but not least"
|
|
|
msgstr ""
|
|
|
|
|
|
-#: ../../guide.rst:983
|
|
|
+#: ../../guide.rst:985
|
|
|
msgid "This is all features of PyWebIO, you can continue to read the rest of the documents, or start writing your PyWebIO applications now."
|
|
|
msgstr "以上就是PyWebIO的全部功能了,你可以继续阅读接下来的文档,或者立即开始PyWebIO应用的编写了。"
|
|
|
|
|
|
-#: ../../guide.rst:985
|
|
|
+#: ../../guide.rst:987
|
|
|
msgid ""
|
|
|
"Finally, please allow me to provide one more suggestion. When you encounter a design problem when using PyWebIO, you can ask yourself a question: "
|
|
|
"What would I do if it is in a terminal program? If you already have the answer, it can be done in the same way with PyWebIO. If the problem "
|
|
@@ -1602,7 +1614,7 @@ msgstr ""
|
|
|
"如果你已经有答案了,那么在PyWebIO中一样可以使用这样的方式完成。如果问题依然存在或者觉得解决方案不够好,\n"
|
|
|
"你可以考虑使用 `put_buttons() <pywebio.output.put_buttons>` 提供的回调机制。"
|
|
|
|
|
|
-#: ../../guide.rst:988
|
|
|
+#: ../../guide.rst:990
|
|
|
msgid "OK, Have fun with PyWebIO!"
|
|
|
msgstr ""
|
|
|
|