RE: Newbie question: SOLVED (how to keep a socket listening), but still some questions

2005-06-24 Thread Giovanni Tumiati
To all those that replied - thank you.
I solved the problem I posted earlier.
I'm embarrassed to admit that it was caused by the following:
...
while 1:  ## wait for a connection
  try:
  #...waiting for connection
  (client, address)=sa.accept()
except sa.timeout: <--there is no such exception for a socket!
 #...waiting for connection timeout
 continue
except:
 continue  ## for now ignore this!
...

There is no "timeout exception" on a socket so the exception was failing.
I don't remember where I read about it, but now I cannot find such an
exception...I guess I dreamt it up :-)

However some of my questions still remain from earlier post:
(1) What is the difference between / how should they be used?
 - setdefaulttimeout(timeout)
 - settimeout(value)

(2)Does one have to do a socket.shutdown() before one does a socket.close??

Again thanks!
pete

-- 
http://mail.python.org/mailman/listinfo/python-list


Re:Another solution to How do I know when a thread quits?

2005-06-08 Thread Giovanni Tumiati
On Tue, 07 Jun 2005 09:41:16 -0400, Peter Hansen wrote:

On Tue, 07 Jun 2005 06:28:33 -0700, Prashanth  Ellina wrote:

> Hi,
> 
> I have used the low-level thread module to write a multi-threaded app.
> 
> tid = thread.start_new_thread(process, ())
> tid is an integer thread ident.
> 
> the thread takes around 10 seconds to finish processing. Meanwhile the
> main thread finished execution and exits. This causes an error because
> the child thread tries to access something in the sys module which has
> already  been GC'ed.  I want a reliable way of knowing when the child
> thread finished execution so that I can make the main thread wait till
> then.
> 

Prashanth,

By reading the Python documentation I figured out to do the following:

import threading

# create an event flag to signal the completion of the thread
task_event=threading.Event()

# create thread
task_thread = threading.Thread\
(None, name_of_thread_function, None, (thread_args...))

# clear the wait for event flag
task_event.clear()

while ...:
# run thread 
try:
task_thread.start()
except:
task_event.set()
error_handler()

if main thread has nothing to do:
# wait for thread to complete (wait on event flag)
task_event.wait()
else:
if task_event.isSet():
# thread has completed
else:
# thread is still active
# so main thread can continue with more tasks

##end while...

Remember to have the "task_thread" set the "task_event" flag prior to
completing.

Hope this helps.

-- 
http://mail.python.org/mailman/listinfo/python-list