同步v/s异步 [英] Synchronous v/s Asynchronous

查看:84
本文介绍了同步v/s异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解龙卷风文档介绍页面上提供的基本示例.它具有2个代码块.同步对我来说很好,我也理解.但是异步的是我无法理解的一种.

I am trying to understand the basic example provided on the introduction page of tornado documentation. It has 2 blocks of code. The Synchronous one is fine for me, and I do understand it. But the asynchronous one is one I am not able to understand.

同步

from tornado.httpclient import HTTPClient

def synchronous_fetch(url):
    http_client = HTTPClient()
    response = http_client.fetch(url)
    return response.body

异步

from tornado.httpclient import AsyncHTTPClient

def asynchronous_fetch(url, callback):
    http_client = AsyncHTTPClient()
    def handle_response(response):
        callback(response.body)
    http_client.fetch(url, callback=handle_response)

如果您可以提供更好的示例,请这样做.

If you can provide with a better example, please do so.

推荐答案

异步调用的思想在许多与Web相关的编程中都非常相似... 东西"(框架,服务器,库...)不仅是Tornado Web服务器中的概念.

The idea of asynchronous calls is something that works pretty much the same in many web related programming... "stuff" (frameworks, servers, libraries...) Is not only a concept from the Tornado web server.

基本思想是:

  • 在s̲y̲n̲c̲h̲r̲o̲n̲o̲u̲s̲请求上,您将发出请求并停止执行程序,直到从HTTP服务器获得响应为止(如果无法访问该服务器则报错,或者如果服务器正在发送则超时)解释器已被阻塞,直到请求完成为止(直到您对请求所发生的事情有明确的答复:进展顺利吗?是否有错误?是否超时?). ).
  • 在一个̲s̲y 启动" 该请求,并且您忘记它" ,这意味着:解释器在执行完之后继续执行代码发出请求无需等待,即可完成请求.

  • On a s̲y̲n̲c̲h̲r̲o̲n̲o̲u̲s̲ request, you make the request and stop executing your program until you get a response from the HTTP server (or an error if the server can't be reached, or a timeout if the sever is taking way, way too long to reply) The interpreter is blocked until the request is completed (until you got a definitive answer of what happened with the request: did it go well? was there an error? a timeout?... ).
  • On a̲s̲y̲n̲c̲h̲r̲o̲n̲o̲u̲s̲ requests, you "launch" the request, and you kind of "forget about it", meaning: The interpreter continues executing the code after the request is made without waiting for the request to be completed.

这似乎……毫无意义,对吧?您将请求发送到空白处" ,然后像往常一样继续执行吗?服务器向您发送响应时会发生什么?我提出了一个请求,我想知道发生了什么!否则,我将不会在代码中以开头输入!!

This seems... rather pointless, right? You send the request "to the void of space", and continue executing as usual? What happens when the server sends you its response? I made a request, and I wanna know what happened to it! Otherwise, I wouldn't have typed that in my code to begin with!!

好吧,这是callback的来源.您启动请求到无空间" 但是,您提供了一个回调函数,因此当HTTP服务器在另一端,它会向您发送响应,该函数将以response作为第一个参数运行.

Well, here's where the callback comes in. You launch the request "to the void of space" BUT you provide a callback function so when the HTTP server on the other end sends you its response, that function is run with said response as the first argument.

我们来看一个en示例.

Let's see it with a en example.

我创建了一个非常简单的龙卷风服务器(使用Python 2.7Tornado 4.2),并且只有一个处理程序.在GET上,需要5秒钟才能返回.我已经使用 time.sleep 做到了,但是在在现实生活中,这可能是一个非常耗时的过程(访问数据库,执行一些计算...谁知道?...

I've created a very simple Tornado server (using Python 2.7 and Tornado 4.2) with only one handler. On a GET, it takes 5 seconds to return. I've done that with a time.sleep, but in real life, it could be a very time consuming process (access a database, perform some calculations... who knows?...)

这是服务器文件(基于Tornado中提供的示例文档):

Here's the server file (based on the example provided in the Tornado documentation):

#!/usr/bin/env python2.7
import time
import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        print "Someone is GET'ing me"
        time.sleep(5)
        self.write("Hello, world")

application = tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    application.listen(8888)
    print "Starting sample server."
    tornado.ioloop.IOLoop.current().start()

打开一个终端并运行该代码以拥有服务器.您将在本地计算机的端口8888上听到龙卷风.

Open a terminal and run that code to have a server. You'll get a Tornado listening on port 8888 of your local machine.

现在,让我们创建另一个脚本(您必须在另一个终端中运行),该脚本以两种方式GET s http://localhost:8888:首先是同步,然后是异步.

Now, let's create another script (which you'll have to run in another terminal) that GETs http://localhost:8888 in two ways: First synchronously and then asynchronously.

#!/usr/bin/env python2.7
import datetime
import tornado.ioloop
from tornado.httpclient import HTTPClient, AsyncHTTPClient


def HUMAN_DT_NOW():
    return datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")


def synchronous_fetch(url):
    http_client = HTTPClient()
    start_dt = datetime.datetime.now()
    response = http_client.fetch(url)
    end_dt = datetime.datetime.now()
    print ("The synchronous fetch took %s seconds."
           % (end_dt - start_dt).total_seconds())
    print "(Sync) Server said: \"%s\"" % response.body


def asynchronous_fetch(url):
    http_client = AsyncHTTPClient()

    def handle_response(response):
        print ""
        print "Yawwza... Finally!!!."
        print "The time now is %s" % HUMAN_DT_NOW()
        print "(Async) Server said: \"%s\"" % response.body
    print "Gonna launch a 'fetch' to the universe at %s..." % HUMAN_DT_NOW()
    http_client.fetch(url, callback=handle_response)

if __name__ == "__main__":
    print " ------ Synchronous ------ "
    print ("Starting synchronous fetch at %s."
           " The program will block for about 5 secs." % HUMAN_DT_NOW())
    synchronous_fetch('http://localhost:8888')
    print "Pfew! That was a lot of wait time!!. I got bored watching my terminal"
    print ""
    print "Aight, let's see what Asynchronous can do"
    print " ------ Asynchronous ------ "
    asynchronous_fetch('http://localhost:8888')
    print "You're gonna see this line before the \"Yawwza...\" one"
    print "This one too. Now is %s" % HUMAN_DT_NOW()
    # The IOLoop below is required to prevent the script from closing ahead
    # of time, but allowing Asynchronous interactions
    tornado.ioloop.IOLoop.current().start()

这将输出:

Starting synchronous fetch at 2015/07/04 13:25:47. The program will block for about 5 secs.
The synchronous fetch took 5.009597 seconds.
(Sync) Server said: "Hello, world"
Pfew! That was a lot of wait time!!. I got bored watching my terminal

Aight, let's see what Asynchronous can do
 ------ Asynchronous ------ 
Gonna launch a 'fetch' to the universe at 2015/07/04 13:25:52...
You're gonna see this line before the "Yawwza..." one
This one too. Now is 2015/07/04 13:25:52

Yawwza... Finally!!!.
The time now is 2015/07/04 13:25:57
(Async) Server said: "Hello, world"

让我们关注异步部分,在这里:

Let's focus on the asynchronous part, here:

 ------ Asynchronous ------ 
Gonna launch a 'fetch' to the universe at 2015/07/04 13:25:52...
You're gonna see this line before the "Yawwza..." one
This one too. Now is 2015/07/04 13:25:52

Yawwza... Finally!!!.
The time now is 2015/07/04 13:25:57
(Async) Server said: "Hello, world"

如果看到的话,该脚本在13:25:52处创建了asynchronous_fetch,但立即(在同一秒内),解释器继续执行,并在发出请求后运行下一条语句(打印You're gonna see this line before the "Yawwza..." oneThis one too. Now is 2015/07/04 13:25:52的行).

If you see, the script made the asynchronous_fetch at 13:25:52, but immediately (in the same second), the interpreter continued executing, and run the next statements after the request was made (the lines that print You're gonna see this line before the "Yawwza..." one and This one too. Now is 2015/07/04 13:25:52 ).

然后,大约5秒钟后,服务器响应,并执行了callback函数(以前是handle_response),这就是您看到的时间

Then, around 5 seconds later, the server responded, and the callback function (which was handle_response) was executed, and that's when you see

Yawwza... Finally!!!.
The time now is 2015/07/04 13:25:57

我希望这有助于对这个想法有所了解.这是一个非常有用的概念,不仅适用于龙卷风.

I hope this helped understanding the idea a bit. It's a very useful concept, and it doesn't only apply to Tornado.

随意使用提供的两个示例脚本,更改内容,增加服务器回复的时间...

Feel free to play with the two sample scripts provided, change stuff, increase the times it gets for the server to reply...

进一步推荐阅读:

  • What is the Asynchronous Web, and How is it Revolutionary?
  • Asynchronous vs. synchronous calls
  • Asynchronous vs synchronous execution, what does it really mean?
  • Tornado's Futures

这篇关于同步v/s异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆