migrate @gen.coroutine AsyncHTTPClient and Motor to native python-3-5-async-and-await

154 views
Skip to first unread message

dziz...@gmail.com

unread,
Jul 27, 2016, 3:28:18 AM7/27/16
to Tornado Web Server
Hi!

I'm working with latests Tornadoweb 4.4.1 and python 3.5 and motor '0.6.2'. I'm intensively use tornado.curl_httpclient.CurlAsyncHTTPClient and Motor Mondodb client.

What would be the benefit to migrate all migrate @gen.coroutine native python-3-5-async-and-await 


Is it risky, worth? Any ideas.

Thanks!

A. Jesse Jiryu Davis

unread,
Jul 27, 2016, 5:14:58 AM7/27/16
to python-...@googlegroups.com
Not risky, Tornado and Motor support async / await very well. You can convert generator-based coroutines to native coroutines one at a time, since Tornado works with a mix of the two styles:

from tornado import gen
from tornado.ioloop import IOLoop
from tornado.locks import Condition

condition = Condition()

@gen.coroutine
def decorated():
    yield gen.sleep(1)
    print("decorated")
    return 1

async def modern():
    result = await decorated()
    print("modern")
    return result

@gen.coroutine
def outer():
    result = yield modern()
    print("outer")
    return result

print(IOLoop.current().run_sync(outer))

Is it worth the trouble? The async / await syntax is nicer and faster. If you use Motor's "cursor.fetch_next" a lot, you might see a measurable performance improvement:


--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dziz...@gmail.com

unread,
Jul 27, 2016, 6:05:44 AM7/27/16
to Tornado Web Server
Thanks! I'll give it a try.

I've seen in your code:

from tornado.locks import Condition

what is the purpose of using it?

I wander, because recently I'm strugling with duplicates in mongo (yielding list of functions and each one might insert same data). I'm creating unique indexes, but then I have to handle DuplicateKeyError exception.

Would Condition be usefull in this case?

A. Jesse Jiryu Davis

unread,
Jul 27, 2016, 8:08:10 AM7/27/16
to python-...@googlegroups.com
I think Condition is too complex for what you need. You have multiple functions inserting the same data sometimes, and that's normal expected behavior for your application? If so you can just catch the error:

try:
    yield collection.insert(doc)
except DuplicateKeyError:
    pass   # Don't care.

Another option is upsert to only insert a document once, then update it if it's already there.

dziz...@gmail.com

unread,
Jul 27, 2016, 9:14:45 AM7/27/16
to Tornado Web Server

Yes, unfortunately remote API I'm using might deliver duplicates, especially when fetched in number of coroutines. I'll proceed with DuplicateKeyError.


Thank you!

A. Jesse Jiryu Davis

unread,
Jul 27, 2016, 9:21:55 AM7/27/16
to python-...@googlegroups.com
Cool. What are you making? I'm always curious about Motor users. 

dziz...@gmail.com

unread,
Jul 28, 2016, 6:26:21 AM7/28/16
to Tornado Web Server
what about yielding lists:

I've implemented in several places: http://www.tornadoweb.org/en/stable/gen.html
response1, response2 = yield [http_client.fetch(url1),
                                  http_client.fetch(url2)]

while awaiting I'm getting: TypeError: object list can't be used in 'await' expression
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+unsubscribe@googlegroups.com.

Ben Darnell

unread,
Jul 28, 2016, 9:16:00 AM7/28/16
to Tornado Web Server
With `await`, lists must be wrapped in `tornado.gen.multi`:

    response1, response2 = await gen.multi([http_client.fetch(url1), http_client.fetch(url2)])


dziz...@gmail.com

unread,
Jul 29, 2016, 2:04:37 AM7/29/16
to Tornado Web Server, b...@bendarnell.com
works like a charm!
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages