network programming without goto

Josiah Carlson jcarlson at uci.edu
Thu Nov 25 12:52:05 EST 2004


kent sin <kentsin at yahoo.com> wrote:
> 
> Please help:
> 
> I was really blocked here. without goto I really do
> not known how to do it.
> 
> The problem is to use PyZ3950 to consult a lists of
> hosts and for each of them to search for a list of
> targets. Since the network is undetermined, there were
> always some exceptions: I would like to allow it to
> retry for 3 times. Moreover, during the query process,
> the conn will timeout (set by the remote server).
> Reconnect is preferred before fail the current search,
> but the reconnect may fail even if the first try is
> succeed.

The trick with getting it to do what you want it to do is to understand
what you want it to do.

Being that I can't understand what you want it to do, the following is
just an interpretation of what I think you want it to do.  If you want
it to do something different, then be clearer with what you want it to
do and ask again.  Note that depending on the semantics of your
connection, this could probably be made more efficient.


queries = [buildquery(t) for t in targets]

for host in hostlist:
    tries_remaining = 3 #tunable parameter
    for q in queries:
        for i in xrange(tries_remaining+1):
            if i == tries_remaining:
                break
            try:
                conn = zoom.Connecton(host.ip, host.port)
                r = conn.search(q)
                conn.close()
                break
            except:
                continue
            
            #handle result r
            break
            
        tries_remaining -= i
        
        if tries_remaining == 0:
            break

 - Josiah




More information about the Python-list mailing list