Skip to content

[Discussion] handle too many request of Entrez #1867

Closed

Description

@hsiaoyi0504

Setup

I am reporting a problem with Biopython 1.72, 3.6, and CentOS 7 as follows:

Traceback (most recent call last):
  File "create_fixture.py", line 127, in <module>
    summary_text, chr_id = search(g)
  File "/var/www/html/Melvin/utils/__init__.py", line 11, in search
    handle = Entrez.esummary(db='gene', id=record['IdList'][0])
  File "/var/www/html/Melvin/env/lib64/python3.6/site-packages/Bio/Entrez/__init__.py", line 320, in esummary
    return _open(cgi, variables)
  File "/var/www/html/Melvin/env/lib64/python3.6/site-packages/Bio/Entrez/__init__.py", line 533, in _open
    raise exception
  File "/var/www/html/Melvin/env/lib64/python3.6/site-packages/Bio/Entrez/__init__.py", line 531, in _open
    handle = _urlopen(cgi)
  File "/usr/lib64/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib64/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/usr/lib64/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib64/python3.6/urllib/request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 429: Too Many Requests

Expected behaviour

Provide a option to avoid this

Actual behaviour

raise HTTPError

Steps to reproduce

invoke request frequently

It's a feature request and I think I can contribute this part.
The reason why I open this is just to have a discussion how should I do that (spec ...).

Activity

  1. peterjc commented on Dec 3, 2018

    @peterjc
    Member

    That should not happen - we already try to enforce most of the NCBI usage restrictions - although as documented, not the For any series of more than 100 requests, do this at weekends or outside USA peak times. rule. See "Entrez Guidelines" in our tutorial:

    http://biopython.org/DIST/docs/tutorial/Tutorial.html
    https://github.com/biopython/biopython/blob/biopython-172/Doc/Tutorial/chapter_entrez.tex#L24

    Perhaps we could add code to do that?

    Related questions: Have you worked out how you triggered this? Did you do lots or requests? Did you have multiple threads, or multiple programs running at the same time, resulting in making Entrez calls too often?

    Were you using an Entrez API key - that allows you to make more calls?

    And assuming you did provide your email address to the NCBI via Entrez, have you had any messages from them about your usage?

  2. hsiaoyi0504 commented on Dec 3, 2018

    @hsiaoyi0504
    ContributorAuthor

    I am thinking if it's good to have another option to set the sleep time inside the function that calls API and when we got such kinds of error, giving some suggestion about that (like what you mentioned, add email, add key, or setting up a sleep time as what I propose here).

    Have you worked out how you triggered this? Did you do lots or requests? Did you have multiple threads, or multiple programs running at the same time, resulting in making Entrez calls too often?

    I didn't introduce those ways such as multiple threads (processes).

    Were you using an Entrez API key - that allows you to make more calls?

    I do know that there is a Entrez API key, but I didn't use them and to make it more general purpose (well, more easier setup), it seems to me that we should not expect users always do that.

    And assuming you did provide your email address to the NCBI via Entrez, have you had any messages from them about your usage?

    I have provided the email, but I didn't get any messages from them about your usage. One interesting thing is that when I run the same snippet (not at the same time) on my computer (MacOS Mojave), it doesn't cause any problem, but when I run it on server (AWS Linux 2 instance), the error occurs. Although the total number of requests is around 200 and it doesn't finish in less than 2 seconds, it raises this error. I do have fixed that by adding sleep time between calls. Then, that's the reason why I am thinking adding this functionality.

  3. peterjc commented on Dec 3, 2018

    @peterjc
    Member

    The NCBI recommends setting up an API key, and then allows up to 10 queries per second. Otherwise they allow up to 3 queries per second. We should update our tutorial to be clearer on this point....

    Anyway, we use those values to set a delay of either 1/10 seconds or 1/3 seconds between calls using sleep here:

    https://github.com/biopython/biopython/blob/biopython-172/Bio/Entrez/__init__.py#L509

    If you are having to add extra sleeps calls, it suggests that isn't working somehow. How long are your extra sleeps between Entrez requests?

    In any case, if you are making 200 requests, you ought not to be doing this during USA working hours. We could add a counter and issue a Python warning if is exceeds 100?

  4. peterjc commented on Dec 3, 2018

    @peterjc
    Member

    It could be that as of 1st December the NCBI are indeed being much stricter - hopefully all we need to do is slightly longer pauses....

    https://www.ncbi.nlm.nih.gov/books/NBK25497/#_chapter2_Usage_Guidelines_and_Requiremen_

  5. peterjc commented on Dec 3, 2018

    @peterjc
    Member

    The tutorial talked about the rate limit twice, 703ec76 simplifies this.

  6. hsiaoyi0504 commented on Dec 3, 2018

    @hsiaoyi0504
    ContributorAuthor

    Initially, I added a fair enough long time (more than 1 sec) to make it works.
    Just few minutes ago, I did a small testing (with less than 100 requests) and it looks like with additional 0.025 should be enough.
    I guess the 3 queries per second is more like 0.36 or 0.37 (pretty weird though).

  7. peterjc commented on Dec 4, 2018

    @peterjc
    Member

    Thank you for looking at this. I can imagine ways the NCBI are measuring this which could explain this - for example they are looking at the interval between calls, there is some fuzziness in the start/end times of each request and its processing.

    Would it be easy for you to try a modified Biopython with the current 0.333333334 delay increased to say 0.37 instead?:

    https://github.com/biopython/biopython/blob/biopython-172/Bio/Entrez/__init__.py#L509

  8. hsiaoyi0504 commented on Dec 5, 2018

    @hsiaoyi0504
    ContributorAuthor

    I have tested and it works for me and create a PR in #1869. However, I am not sure if we should expose the delay time. One of possible conditions is that user get permission from NCBI with higher rate-limit and want to decrease the delay time.

  9. peterjc commented on Dec 5, 2018

    @peterjc
    Member

    Thank you.

    The NCBI have already created the API key approach to allow the higher rate-limit of 10 requests per second, so I don't think we need to expose this as an option.

    If anyone really want to slow down calls to Entrez, it is easy to add explicit calls to sleep(...) in the user's code.

  10. hsiaoyi0504 commented on Dec 5, 2018

    @hsiaoyi0504
    ContributorAuthor

    Sorry, perhaps I didn't describe clearly. What I mean earlier is that if an organization / user got higher rate permission and it's based on Biopython, they may want to decrease the delay time. That is, speeding up
    rather than slowing down.

    By including an API key, a site can post up to 10 requests per second by default. Higher rates are available by request

    from https://www.ncbi.nlm.nih.gov/books/NBK25497/?report=reader

  11. peterjc commented on Dec 5, 2018

    @peterjc
    Member

    Ah - I see what you mean now. I would wait until someone actually sets that up and can actually test it. Thanks for the clarification.

  12. hsiaoyi0504 commented on Dec 6, 2018

    @hsiaoyi0504
    ContributorAuthor

    Thanks.

  13. hsiaoyi0504 commented on Dec 12, 2018

    @hsiaoyi0504
    ContributorAuthor

    @peterjc I just re-oepn this because my use case reminds me that even general users may still require adjusting delay time. In my current project, now it has fair long enough time (due to other procedure that processes data) between two requests. Then, I would like to decrease the delay time to make it a little bit faster. Do you have any idea ?

  14. peterjc commented on Dec 12, 2018

    @peterjc
    Member

    The code as written looks at the system clock via time.time(), so if your code has been running some other analysis and it has taken for example 20s since the last Entrez call, it will deduct that from the sleep delay. i.e. With your change from #1869, 37s - 20s = only 17s sleep.

    If your script/program has been doing lots of other data analysis and more than 37s have passed, Biopython will not sleep at all, and will call Entrez immediately:

        current = time.time()
        wait = _open.previous + delay - current
        if wait > 0:
            time.sleep(wait)
            _open.previous = current + wait
        else:
            _open.previous = current

    Are you saying now you want it to be a little faster than 37s, maybe 36s, or even go back to 0.333333334s?

    If you want it to be a lot faster than the new 37s delay, you should use the Entrez API key which allows for a 10s delay.

  15. hsiaoyi0504 commented on Dec 13, 2018

    @hsiaoyi0504
    ContributorAuthor

    @peterjc Oh, you are right. I forget there is a conditional there. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions