<div dir="ltr"><div>Hi,</div><div><br></div><div><b>Disclaimer</b>: I haven't used ncbiWWW module.</div><div><br></div><div><div><b>Suggestion 1</b>: if you're using a *NIX system. Can make use of Signals. Wrap your call with the signal. Define the signal handler:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">import signal<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">class timeout:<br>    def __init__(self, seconds=1, error_message='Timeout'):<br>        self.seconds = seconds<br>        self.error_message = error_message<br>    def handle_timeout(self, signum, frame):<br>        raise TimeoutError(self.error_<wbr>message)<br>    def __enter__(self):<br>        signal.signal(signal.SIGALRM, self.handle_timeout)<br>        signal.alarm(self.seconds)<br>    def __exit__(self, type, value, traceback):<br>        signal.alarm(0)</blockquote><br>The above class will raise <b>TimeoutError </b>after the specified time (seconds), to wrap your call:<br>try:<br>       with timeout(seconds=10):  # the time you expect as normal<div>             <your <span style="font-size:12.8px">ncbiwww.qblast() call</span>></div><div>except TimeoutError as exc:<br>        <your second <span style="font-size:12.8px">ncbiwww.qblast() call</span>></div><div>finally:<br>        <optional block><br><br><br></div></div><div><b>Suggestion 2</b>: using Multiprocessing or multithreading - for it, kindly share your script/snippet.<br></div><div><br></div><div><br></div><div><b>Suggestion 3</b>: Make direct API calls using '<b>requests</b>' package.</div><div>In case the API calls are simple (you can easily do so) use request to make a call, with timeout flag, once the HTTP request will timeout it'll raise Timeout exception, which you can catch and in that block make the second call (which as per you, works perfectly fine):</div><div><br></div><div><div>try:</div><div>      resp = requests.get(blast_query_url, timeout=10.0).content </div><div>except requests.exceptions.<b>Timeout</b>:</div><div>      resp = requests.get(blast_query_url, timeout=10.0).content </div></div><div><br></div><div>After 10 seconds the call will raise Timeout, which is handled, and makes the call again.</div><div><br></div><div>Cons:</div><div><ul><li>Not much elegant - coding overhead.<br></li><li>What if the second request also take more than the timeout<br></li></ul></div><div><br></div><div>Cheers,</div><div>Nabeel.</div><div><br></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 26, 2017 at 4:26 PM, Pejvak Moghimi <span dir="ltr"><<a href="mailto:pejvak.moghimi@york.ac.uk" target="_blank">pejvak.moghimi@york.ac.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span style="font-size:12.8px">Hi all,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I'm working on a script to to do blast using the ncbiWWW module, but very often one query sequence takes way too long, if at all, to return with results. This is always, in my experience, immediately solved if I just stop the script and re-run it (for the same query sequence); I get the results as quickly as I did for the other query sequences.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I think this hints that this is something to do with ncbi servers. So, in order to tackle it, I need to simply modify my while loop to stop and re-run the "ncbiwww.qblast(..." line, if it takes longer than a reasonable length of time (I do understand shorter than a certain waiting-time would not be allowed by ncbi).</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I have no idea how to tackle this, except by either multithreading (not so sure how to go on about this though) or changing the qblast script (locally of course).</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I would really appreciate any help. Please do let me know if you would like to have a look at the script.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Cheers,</div><div style="font-size:12.8px">Pej.</div></div>
<br>______________________________<wbr>_________________<br>
Biopython mailing list  -  <a href="mailto:Biopython@mailman.open-bio.org" target="_blank">Biopython@mailman.open-bio.org</a><br>
<a href="http://mailman.open-bio.org/mailman/listinfo/biopython" rel="noreferrer" target="_blank">http://mailman.open-bio.org/ma<wbr>ilman/listinfo/biopython</a><br></blockquote></div><br></div></div></div></div>