[Tutor] sockets, files, threads
Marilyn Davis
marilyn at deliberate.com
Wed Jan 19 20:53:24 CET 2005
Thank you Kent.
On Wed, 19 Jan 2005, Kent Johnson wrote:
> Marilyn Davis wrote:
> >>few lines up, right where 'client_socket' is initialized. Like this:
> >>
> >>###
> >> try:
> >> client_socket, client_addr = self.server_socket.accept()
> >> Spawn(client_socket).start()
> >> except socket.error, msg:
> >> time.sleep(.5)
> >> except (EOFError, KeyboardInterrupt):
> >> self.close_up()
> >>###
> >>
> >>Not only does this make it more clear where 'client_socket' is being used,
> >>but it ends up making the code shorter. I've dropped the 'continue'
> >>statement, as it becomes superfluous when the Spawn() moves into the try's
> >>body.
> >
> >
> > But but but, that wraps the whole thread in one try/except clause.
> > That Spawn() call starts the thread. If a client_socket generates an
> > error, we don't want the main thread to sleep. All the socket
> > operations in Spawn.start() are also wrapped in their own (hopefully)
> > intelligent try/excepts. I thought it was good practice to -not- put
> > extra stuff in a try/except.
>
> Use try: except: else:
> The else clause is only executed if no exception happens, so the code is correct; the else is
> outside the scope of the try, so you don't catch unexpected exceptions.
>
> try:
> client_socket, client_addr = self.server_socket.accept()
> except socket.error, msg:
> time.sleep(.5)
> except (EOFError, KeyboardInterrupt):
> self.close_up()
> else:
> Spawn(client_socket).start()
That makes good sense. But I'm not threading anymore and my call to
server_socket.accept now blocks so I'm not sleeping. The only
exceptions I want to catch now are those keyboard things, which make
me stop. The socket.error I was catching was that there was no
connections waiting right now. So my situation is much simpler.
Marilyn
>
> Kent
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
More information about the Tutor
mailing list