1

I have a REST API and now I want to create a web site that will use this API as only and primary datasource. The system is distributed: REST API is on one group of machines and the site will be on the other(s).

I'm expecting to have quite a lot of load, so I'd like to make requests as efficient, as possible.

Do I need some async HTTP requests library or any HTTP client library will work?

API is done using Flask, web site will be also built using Flask and Jinja as template engine.

artvolk
  • 9,448
  • 11
  • 56
  • 85

3 Answers3

2

You could use gevent with Flask to get asynchronous I/O from normally synchronous libraries. See this question for an example of someone getting help with doing that.

You could also run Flask behind gunicorn, which has support for spawning multiple workers (threads, processes, or greenlets) for handling concurrent requests. If you were to take that approach, Flask would remain completely synchronous, and gunicorn would handle creating multiple Flask instances to handle concurrent requests.

Community
  • 1
  • 1
dano
  • 91,354
  • 19
  • 222
  • 219
  • I do use gunicorn already. It seems I can combine gevent with gunicorn and get async I/O in every worker I run? – artvolk May 14 '14 at 19:30
  • 1
    @artvolk If you're already using gunicorn, you may be able to get async I/O simply by switching to async workers: See here http://gunicorn-docs.readthedocs.org/en/latest/design.html#choosing-a-worker-type and here: http://gunicorn-docs.readthedocs.org/en/latest/design.html#async-workers – dano May 14 '14 at 20:22
  • It seems that for my task I just need to use async workers in gunicorn, thanks! – artvolk May 15 '14 at 01:56
1

Start simple and use the way, which seems to be easy to use for you. Consider optimization to be done later on only if needed.

Use of async libraries would come into play as helpful if you would have thousands of request a second. Much sooner you are likely to have performance problems related to database (if you use it), which is not to be resolved by async magic.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • API use a lot of caching, so the DB will not be a bottleneck for the most of the load, I think about caching on the frontend web site too. – artvolk May 14 '14 at 15:00
0

Actually your API is on a separate machine. Even if you make your client calls asynchronous , it will not have any impact on the server. By making your calls asynchronous, your thread in the client will not wait for the response. Your server will react the same when call is sync /async.

And if you want to make your calls async , please check http://stackandqueue.com/?p=57 . It uses unirest to make both get and post async calls

gvir
  • 256
  • 3
  • 4