4

I am experimenting with gunicorn with gevent workers and Flask. In a view function, I need to make a GET request to an url. Previously, I'm using requests library to do that. But since I want to use gevent workers, I believe I have to make it async, so I use grequests library.

This is my code:

from gevent import monkey
monkey.patch_all()

import grequests
from flask import Flask
from flask import jsonify

pool = grequests.Pool()


@app.route('/<search_term>')
def find_result(search_term):
    return get_result(search_term) or ''


def get_result(search_term):
    request = grequests.get('https://www.google.com/search?q=' + search_term)
    job = grequests.send(request, pool=pool)
    async_resp = job.get()  # this blocks
    resp = async_resp.response
    if resp.status_code == 200:
        return resp.text

Is this the correct way to use grequests, since I use blocking job.get()? I want to exploit the fact that the problem I have is IO bound.

hans-t
  • 3,093
  • 8
  • 33
  • 39

0 Answers0