While loop

J Kenneth King james at agentultra.com
Thu Mar 5 15:09:32 EST 2009


Fab86 <fabien.hall at gmail.com> writes:

> On Mar 5, 5:23 pm, Marco Mariani <ma... at sferacarta.com> wrote:
>> Fab86 wrote:
>> > Is it possible to get the program to catch the exception, wait 10
>> > seconds, then carry of from where it was rather than starting again?
>>
>> something like this? probably works in PASCAL as well :)
>>
>> > i=0
>> > while i < len(stuff):
>> >    try:
>> >       do_with(stuff[i])
>> >    except SomeError:
>> >       sleep(10)
>> >       continue
>> >    i+=1
>>
>>
>
> using sleep and then continue just makes the search start from the
> first search term like before.. Would it be easier to understand if I
> posted sections of my code?
>
> i = 0
> k = open('blah', 'w')
> domains = ["au", "ca", "nl", "be", "...]
> srch = WebSearch(app_id=YahooKey)
>
> while i<200:
>     try:
>         for domain in domains:
>             srch.query = "test site:.%s" % domain
>             res = srch.parse_results()
>             print >> k, res.total_results_available
>             i = i + 1
>
>     except SearchError:
>
> (I currently close then reopen document here then restart i to 0)
>
> Any ideas?

You should check out the sched module for scheduling tasks.

<code>

import sched, time

domains = ["au", "ca", "nl", "be", "ru", "us", "de"]
s = sched.scheduler(time.time, time.time)

def do_query(query, domain):
    search.query = "%s site:.%s" % (query, domain)
    try:
        result = search.parse_results()
    except SearchError, e:
        print >> sys.stderr, e
    else:
        print >> k, result

for domain in domains:
    s.enter(2, 1, do_query, domain)

s.run()

</code>

YMMV



More information about the Python-list mailing list