Am I crazy or is it actually going this fast? (MultiThreading)
<pre><code>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))
</code></pre>

<p>Result <code>1.11500000954 seconds</code></p>

<p>I wrote this to see how fast 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 its not actually going this quick, if I did do something wrong, could someone point out my error?</p>

<p>Thank you!</p>

<p>As the other answers already pointed out, <code>tornado</code> works asynchronously. You have to run the requests in an <a href="http://www.tornadoweb.org/en/stable/ioloop.html#ioloop-objects" rel="nofollow"><code>IOLoop</code></a>. Please note that this has nothing to do with multithreading. The requests are executed in parallel, but within a single thread (the core feature of async IO).</p>

<p>This is how it could be done:</p>

<pre><code>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 &gt;= 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()
</code></pre>

<p>Your code only creates 1000 request objects (<a href="http://www.tornadoweb.org/en/stable/concurrent.html#tornado.concurrent.Future" rel="nofollow"><code>Future</code></a> objects to be precise), that are actually never send to the server.</p>

python multithreading tornado