network programming without goto
Josiah Carlson
jcarlson at uci.edu
Mon Nov 29 00:22:00 EST 2004
kent sin <kentsin at yahoo.com> wrote:
>
> But that will make too many uncessary connection:
>
> one connection can do a number of search before it
> got timeout, so I want to do as many search as
> possible before it got timeout. I think the connection
> cost is high here, and it also got some load on the
> remote servers which I do not want.
>
> Thanks for the suggestion.
Indeed it will do many connections, but you weren't terribly specific
with what you wanted.
In any case, if you want a total of 3 failures under any situation, try
the following.
queries = [buildquery(t) for t in targets]
tries = 3
for host in hostlist:
q = iter(queries)
try:
cq = q.next()
except StopIteration:
continue
for i in xrange(tries):
conn = zoom.Connecton(host.ip, host.port)
fail = 0
while 1:
try:
r = conn.query(cq)
#deal with r
cq = q.next()
except StopIteration:
break
except:
fail = 1
break
if not fail:
break
try:
conn.close()
except:
pass
- Josiah
>
> --- Josiah Carlson <jcarlson at uci.edu> wrote:
>
> >
> > 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