tonysepia
tonysepia

Reputation: 3510

Python gevent+bottle. Querying an API. How to use gevent to prevent timeout locks?

I'm using gevent + bottle for following:

  1. call API method on remote server
  2. Process result from the API
  3. return HTML

I've set a tiemout for the API call (httplib/socket), but if it's set to 5 seconds (for example), my python script is busy for that time and can't return any other pages (which is normal).

Question: Can I somehow make a clever use of gevent (in a separate script, maybe?) to handle such long requests?

I was thinking of starting a separate API-interrogating script on localhost:8080 and putting it behind a load balancer (as "Internet" suggested) but I'm sure there msut be a better way.

I am not an experienced programmer, so thank you for your help!

Upvotes: 3

Views: 4327

Answers (1)

defnull
defnull

Reputation: 4199

Actually, your problem should not exist. The gevent server backend can handle any number of requests at the same time. If one is blocked for 5 seconds, that does not affect the other requests arriving at the server. Thats the point of the gevent server backend.

1) Are you sure that you use the gevent server backend properly? And not just a monkey-patched version of the wsgiref default server (which is single-threaded)?

2) Did you start the server via bottle.py --server gevent? If not, did you gevent.monkey.patch_all() before importing all the other socket-related stuff (including bottle)?

Example:

from gevent import monkey
monkey.patch_all()
import bottle
import urllib2

@bottle.route(...)
def callback():
   urllib2.open(...)

bottle.run(server='gevent')

Upvotes: 11

Related Questions