[Tutor] Changing instance attributes in different threads

Bernard Lebel 3dbernard at gmail.com
Tue Feb 7 04:31:26 CET 2006


Hi Kent,

I have put together a little script to give a rough idea about what
the program does.

http://www.bernardlebel.com/scripts/nonxsi/help/bl_threadtest.py


The true program does this:

- the top program file imports a module called fcJob

- the top program instantiate the only class in the fcJob module. The
class is named fcJob as well, the instance is named simply "job". This
instance has few attribute, the one that I'm interested in now is
"localjobstatus".

- the top program file enters a while loop where it checks a variety
of things, and if certain conditions are met, will start a big
function in a separate thread.

- the function that runs in the separate thread reads and writes the
localjobstatus attributes.

- while the child thread is running, the main thread checks a database
every 5 seconds to test the value of certain fields.

- would the value of specific field changed to certain values, the top
thread will set the localjobstatus value to something like "killed".

- the child thread, also running a while loop, tests the local job
attribute at 3 times during a single iteration. If it gets a "Killed"
value, it will call a function that basically terminates this child
thread in a clean way. Ultimately, it will set the localjobstatus to
"Pending".

So in essence, there are two thread reading and writing the the
localjobstatus attribute, the main thread and a child thread. The
child thread is reading the value in order to control its flow.

Hope I'm making sense.


Thanks
Bernard



On 2/6/06, Kent Johnson <kent37 at tds.net> wrote:
> Bernard Lebel wrote:
> > Example:
> >
> > - Class instance Bernard has attribute "name", whose value is "bernard".
> > - A function in a thread tests the value of "name". If "name" ==
> > "bernard", do nothing.
> > - A function in another thread, for some reason, changes "name" to "bob".
> > - The first function, few moments later, tests again the value of
> > "name". It see that the value has changed from "bernard" to "bob", and
> > this change causes the function to take an action.
> >
> > Is that what you mean by "invoking" code?
>
> No, it's not what I meant, I was talking about constructs that can cause
> an assignment like self.x = 1 to directly call code that you wrote. It
> sounds like you are not doing that.
>
> It sounds like you have some attributes that you are using as flags to
> allow one thread to control another. There are definitely some pitfalls
> here. You probably want to use threading.Condition or Queue.Queue to
> communicate between the threads. Can you give more details of what you
> are trying to do?
>
> Kent
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list