Various issues regarding thread

Aahz Maruch aahz at panix.com
Thu Mar 29 00:36:47 EST 2001


In article <mailman.985116565.4789.python-list at python.org>,
Gabriel Ambuehl  <gabriel_ambuehl at buz.ch> wrote:
>
>I've been developping a small resource monitoring syste, in Python in
>the last time and got some questions regarding threading.thread. I
>figured it would probably be the easiest way (forking of new processes
>seems very unwise as RAM to useage) to have a dedicated thread for
>every resource that has to be monitored so it would be possible to just
>have them sleep between the work and let Python do the scheduling. Now
>however, I thought it could be cool to have the thing using pickle to
>dump its state before it exits so it could continue its work right
>where it left last time (not yet sure if this makes much sense though
>;-). The only problem there is that pickle doesn't appear to be working
>on lock objects which the thread subclasses inherently got. So is there
>any possibility to serialize a class that has been inherited from
>threading.thread?

I haven't tested this, so I don't know whether pickle can handle lock
objects (though it makes sense to me that they can't).  What might make
sense for you would be to create a "worker" class that isn't directly
thread-aware (but is thread-safe), which you can then pickle, and have
the thread class do all the locking around method calls to the worker
class.

>Another thing I couldn't figure out is how one could stop a thread
>reliably. I first thought one could have a flag that gets checked by
>the run method every now and then but then found this to be to slow as
>it could well be that a thread sleeps for several minutes before it
>notices that the flag had been set and thus should have exited some
>time ago. What am I overlooking?

Have the resource threads wait on a Queue.Queue().  Have one timer
thread that puts work/die tokens on the queue.
-- 
                      --- Aahz  <*>  (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het Pythonista   http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"Boost the stock market -- fire someone"



More information about the Python-list mailing list