Learning to use threads.

Peter Hansen peter at engcorp.com
Sun Jan 26 11:25:37 EST 2003


Grumfish wrote:
> 
> Thank you. A follow up question: is it safe to call a custom method of a
> running Thread?

Yes.  Or no, depending on what it does.

Presumably you are talking about calling a method defined in
a subclass of threading.Thread, from another thread...  if
that's the case, you have to understand that because this
method is being called from another thread of execution, you
might have to deal with synchronization issues, to make sure
you don't end up with data structures in an undefined state.

If you are trying to communicate with the running Thread
(which is of course not running at the instant you call it
because the thread from which you are calling it is the one
running :-), and you stick with the Queue object as your 
means of communicating, then in general you should be safe.

If, however, this custom method starts directly manipulating data
in the thread object, you could quickly get into trouble if you
don't know about thread synchronization issues.  If that's what
you need, and you won't for some reason use a Queue, you'll
need to learn more about Semaphores or RLocks or such things
to keep yourself out of trouble.  Better just to stick with 
Queue...

-Peter




More information about the Python-list mailing list