Am I crazy or is it really that fast? (MultiThreading)

from tornado import httpclient
import time

start = time.time()

for x in range(1000):
    httpclient.AsyncHTTPClient().fetch("https://www.google.com", method="GET")

print ('{0} seconds'.format(time.time() - start))

Result 1.11500000954 seconds

I wrote this to find out how quickly I can send 1000 requests to any site (I chose google), and I don’t know why, but I feel like I did something wrong, and actually it’s not so fast. if I have something wrong, can someone point out my mistake?

Thank!

+4
source share
3 answers

Well, yes, you allocated 1000 asynchronous requests to Google and timed it. However, you did not run into overhead by actually calling an HTTP call. This will require a callback-type handler.

+5
source

, , fetch() - , , - , , . , , , ~ 1 , - , , . , , , , , / - , , - " ".

, , , , , , , .

0

, tornado . IOLoop. , . , ( async IO).

:

from tornado import httpclient, ioloop
import time

start = time.time()

loop = ioloop.IOLoop.instance()
N = 10
finished = 0

def callback(f):
    global finished
    finished += 1
    print('%d requests finished' % finished)
    if finished >= N:
        loop.stop()
        print ('{0} seconds'.format(time.time() - start))

for x in range(N):
    f = httpclient.AsyncHTTPClient().fetch("https://www.google.com",
                                           method="GET")
    loop.add_future(f, callback)

loop.start()

1000 (Future , .

0

Source: https://habr.com/ru/post/1652734/


All Articles