Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

理解 Python 中的异步编程 #1875

Merged
merged 7 commits into from
Jul 19, 2017
Merged

理解 Python 中的异步编程 #1875

merged 7 commits into from
Jul 19, 2017

Conversation

steinliber
Copy link
Contributor

No description provided.

@CACppuccino
Copy link
Contributor

@steinliber @sqrthree 校对认领

@linhe0x0
Copy link
Member

@CACppuccino 好的呢 🍺

@linhe0x0 linhe0x0 changed the title 完成理解 Python 中的异步编程的翻译 理解 Python 中的异步编程 Jul 12, 2017
Copy link
Contributor

@CACppuccino CACppuccino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steinliber @sqrthree 基本没有问题,校对完毕


- **Batch processing programs** are often created as synchronous programs: get some input, process it, create some output. One step logically follows another till we create the desired output. There’s really nothing else the program has to pay attention to besides those steps, and in that order.
- **批量处理程序**通常是写成同步程序的:获取一些输入,处理它,然后创建一些输出。从逻辑上来说,直到我们创建了想要的输出,每一步都是另一步完成之后开始执行的。除了这些执行步骤和执行的顺序外,这个程序并没有其他值得注意的地方。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[执行的步骤和执行的顺序]=>[执行的步骤和顺序]


An **asynchronous program** behaves differently. It still takes one execution step at a time. However the difference is the system may not wait for an execution step to be complete before moving on.
一个**异步程序**表现的就不一样。它仍然在同一时间只执行一步。然而其不同之处在于系统可能不会等到一个执行步骤完成后再执行下一步。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[它仍然在同一时间只执行一步。] =>[它仍然是每次执行一步]


Here’s a conceptual program that might be a candidate for asynchronous programming:
这里有个概念上的程序或许是可以使用异步的候选者:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[这里有个概念上的程序或许是可以使用异步的候选者:] => [这里有个概念性的程序可以作为认识异步编程的例子:]


**What are the real limits of optimizing the above approach?** The speed of the network, file IO speed, database query speed, the speed of other connected services, etc. The common feature of this list is they are all IO functions. All of these items are many orders of magnitude slower than our CPU’s processing speed.
**上述方法优化的真实限制是什么?** 网络的速度,文件的 IO 传输速度,数据库的查询速度,其它连接服务的速度等。这个列表的的共有特征是它们都是 IO 函数。所有这些项的处理速度都比我们的 CPU 处理速度要慢很多个数量级。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[文件的 IO 传输速度] => [文件的读写速度]


For *batch oriented programs* this isn’t a priority, processing the results of that IO is the goal, and often takes far longer than the IO. Any optimization efforts would be focused on the processing work, not the IO.
对于**面向批量处理的程序**这并不是关键,它的目的是处理通过 IO 操作得到的结果,而且这个过程所花时间通常比 IO 操作长得多。任何优化的工作都将侧重于处理的工作而不 IO。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[而不 IO] =》 [而不是 IO。]


If we think of all the tasks as one “program”, we can break up the tasks and run them as threads, cloning the parent so to speak. Now there is a parent instance for each task; watching the kids, monitoring the dryer, monitoring the washer and doing the checkbook, all running independently. This sounds like a pretty nice solution to the program problem.
如果我们把所有的任务看成一个"程序",我们就可以把这些任务分解出来并且使用线程来运行这些任务,只要克隆这个父母就可以了。现在对于每个任务都有一个父母实例;包括照看孩子,看管洗衣机,看管烘干机和平衡收支本,所有这些任务都是独立运行的。这听起来对于这个程序的问题来说是一个很棒的解决方案。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[这听起来对于这个程序的问题来说是一个很棒的解决方案。] => [这听起来对程序来说是个不错的解决方案。]


This is a form of cooperative concurrency. The program is yielding control of its current context so something else can run. In this case it allows our primative “run the tasks” scheduler to run two instances of the task function, each one consuming work from the same queue. This is sort of clever, but a lot of work to get the same results as the first program.
这是一种协作并发的方式。这个程序会让出对它当前上下文的控制,这样其它的任务就可以运行。在这种情况下,它允许我们主要的 "run the tasks" 调度器可以运行任务函数的两个实例,每一个实例都从相同的队列中消费工作。这的确有点聪明,但是做了这么多得到的却是和第一个程序一样的结果。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[这的确有点聪明,但是做了这么多得到的却是和第一个程序一样的结果。] => [这种做法虽然聪明一些,但达成同样结果的同时做了更多的工作。]

@@ -235,15 +236,16 @@ if __name__ == '__main__':
main()
```

When this program is run the output shows that both task one and two are running, consuming work from the queue and processing it. This is what’s intended, both tasks are processing work, and each ends up processing two items from the queue. But again, quite a bit of work to achieve the results.
当程序运行时,输出表明任务一和任务二都在运行,它们都从队列里消耗工作并且处理它。我们想要的是两个任务都在处理工作,每一个都以处理从队列中的两个项目结束。但是再一次,需要做一点工作来实现这个结果。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[我们想要的是两个任务都在处理工作] =>[这就是我们想要的,两个任务都在处理工作,。。。。]


We’ve modified the program to import the wonderful [`requests` module](https://requests.org/) to make the actual HTTP requests, and added a list of URLs to the queue rather than numbers. Inside the task, rather than increment a counter, we’re using the requests module to get the contents of a URL gotten from the queue, and printing how long it took to do so.
我们修改了这个程序导入了非常棒的 [`requests` 模块](https://requests.org/) 来创建真实的 HTTP 请求,而且我们把一份 URL 列表加入到队列中,而不是像之前一样只是数字。在这个任务中,我们也没有再增加计数器,而是使用 requests 模块来获取从队列里得到 URL 页面的内容,并且我们打印了执行这个操作的时间。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[我们也没有再增加计数器] => [我们也没有再用计数器](做数字上的增加)不过括号里的内容不用翻译出来


Doug is a Python developer with more than 20 years of experience. He writes about Python on his [personal website](http://writeson.pythonanywhere.com/) and currently works as a Senior Web Engineer at Shutterfly. Doug is also an esteemed member of [PythonistaCafe](https://www.pythonistacafe.com).
Doug 是一位具有二十多年开发经验的 Python 开发者。他在他的[个人网站](http://writeson.pythonanywhere.com/)上写关于 Python 的文章,目前在Shutterfly担任高级Web工程师。 Doug也是 [PythonistaCafe](https://www.pythonistacafe.com) 中的一位值得尊敬的成员。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

中英文空格

@MrShayne
Copy link

@steinliber @sqrthree 校对认领

@linhe0x0
Copy link
Member

@MrShayne 妥妥哒 🍻


Here are couple of example programs that would work this way:
这里是使用这种模式运行的示例程序:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下面是使用这种模式运行的示例程序


- **Batch processing programs** are often created as synchronous programs: get some input, process it, create some output. One step logically follows another till we create the desired output. There’s really nothing else the program has to pay attention to besides those steps, and in that order.
- **批量处理程序**通常是写成同步程序的:获取一些输入,处理它,然后创建一些输出。从逻辑上来说,直到我们创建了想要的输出,每一步都是另一步完成之后开始执行的。除了这些执行步骤和执行的顺序外,这个程序并没有其他值得注意的地方。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[从逻辑上来说,直到我们创建了想要的输出,每一步都是另一步完成之后开始执行的。除了这些执行步骤和执行的顺序外,这个程序并没有其他值得注意的地方] => [按照逻辑一步接一步地运行直到得到我们想要的输出。除了执行的步骤内容和顺序外,这类程序并不需要关注任何其他事情。]


- **Command-line programs** are often small, quick processes to “transform” something into something else. This can be expressed as a series of program steps executed serially and done.
- **命令行程序**通常是一个小而快的程序,用来把一些东西“转换“成其他一些东西。这个程序的执行可以表示成一系列程序步骤连续执行后完成任务。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[这个程序的执行可以表示成一系列程序步骤连续执行后完成任务。] => [这个程序表现出来就是一系列的步骤顺序执行直到完成。]

Copy link
Member

@linhe0x0 linhe0x0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一些小问题麻烦调整下哈。辛苦啦。

@@ -1,130 +1,131 @@
> * 原文地址:[Understanding Asynchronous Programming in Python](https://dbader.org/blog/understanding-asynchronous-programming-in-python)
> * 原文作者:[Doug Farrell](https://dbader.org/blog/understanding-asynchronous-programming-in-python#author)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 译者:
> * 译者:[steinliber](https://github.com/steinliber)
> * 校对者:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的校对者信息要加上啊


Even with conditional branching, loops and function calls, we can still think about the code in terms of taking one execution step at a time, and when complete, moving on to the next.
即使是有条件分支,循环和函数调用,我们仍然可以认为代码在同一时间只执行一步,当这一步完成时,才执行下一步。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『条件分支,循环和函数调用』=>『条件分支、循环和函数调用』


**Why?** In the case of a web server one unit of work (input, process, output) is not its only purpose. Its real purpose is to handle hundreds, perhaps thousands, of units of work at the same time, and for long periods of time.
**为什么?** 在一个 web 服务器的情况下,一个工作单元(输入,处理,输出)不是这个服务器唯一的目的。它真正的目的是能长时间同时处理数百上千个工作单元。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『输入,处理,输出』=>『输入、处理、输出』


**Here’s an example a lot of you can relate to:** being a parent trying to do several things at once; balance the checkbook, do some laundry and keep an eye on the kids.
**这里有一个大多数人都相关联的例子:** 作为一个父母尝试同时做好几件事;包括平衡支票本,洗衣服和照看孩子。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『平衡支票本,洗衣服』=>『平衡支票本、洗衣服』


## Thought Experiment #2: The “Polling” Parent
## 头脑风暴 #2:"轮询"的父母
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这三个标题,有用全角冒号的,有用半角的,有后面加空格的,有不加的。还是统一下吧。


## Example 1: Synchronous Programming
## 示例 1:同步编程
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的标题也是。


You can download all of the example code from [this GitHub repository](https://github.com/writeson/async_python_programming_presentation).
你可以在这个 [Github 仓库](https://github.com/writeson/async_python_programming_presentation) 下载所有的实例代码。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『Github』=>『GitHub』

@steinliber
Copy link
Contributor Author

已经根据校对意见进行了修改,非常感谢 @sqrthree @CACppuccino @MrShayne 的建议🙏🙏

@linhe0x0 linhe0x0 merged commit 92ec9ce into xitu:master Jul 19, 2017
@linhe0x0
Copy link
Member

@steinliber 已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。

RichardLeeH pushed a commit to RichardLeeH/gold-miner that referenced this pull request Jul 19, 2017
…slate

# By 根号三 (30) and others
# Via 根号三
* 'master' of https://github.com/xitu/gold-miner: (52 commits)
  ✨ Create how-to-implement-expandable-menu-on-ios-like-in-airbnb.md
  ✨ Create the-future-of-deep-learning.md
  ✨ Create the-limitations-of-deep-learning.md
  ✨ Create deep-learning-3-more-on-cnns-handling-overfitting.md
  ✨ Create deep-learning-2-convolutional-neural-networks.md
  ✨ Create deep-learning-1-setting-up-aws-image-recognition.md
  ✨ Create user-breakpoints-in-xcode.md
  ✨ Create reflections-on-eslints-success.md
  ✨ Create next-generation-3d-graphics-on-the-web.md
  Update react-redux-optimization.md
  语句,消息和归约器 (xitu#1882)
  理解 Python 中的异步编程 (xitu#1875)
  如何处理机器学习中的不平衡类别 (xitu#1870)
  Redux 并不慢,只是你使用姿势不对 —— 一份优化指南 (xitu#1897)
  修改一个拼写错误
  即将到来的正则表达式新特性 (xitu#1883)
  🚀 添加文章『 将 Kotlin 应用于现有 Java 代码的策略』到文章列表
  🚀 添加文章『是的,ES2017 (ES8)来了』到文章列表
  Update es8-was-released-and-here-are-its-main-new-features.md
  ✨ Create pyqt-versus-wxpython.md
  ...
cdadar pushed a commit to cdadar/gold-miner that referenced this pull request Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants