[Tutor] thread locks

Rick Pasotto rick@niof.net
Mon, 3 Dec 2001 23:41:02 -0500

On Mon, Dec 03, 2001 at 08:28:28PM -0500, dman wrote:
> On Mon, Dec 03, 2001 at 04:39:38PM -0500, Rick Pasotto wrote:
> | Or is there a better way to solve my problem?
> import threading , time
> def func() :
>     while 1 : pass # do something that takes a while
> t = threading.Thread( target=func )
> t.start()
> while t.isAlive() : time.sleep( 1 )

This works with one addition:

while t.isAlive() :
    time.sleep( 1 )

> | I know this seems to defeat the purpose of using a thread but this is
> | in a Tkinter program and if I don't do this the main window will not
> | get redrawn if the user covers it up, etc.
> Depending on how this is used and what it affects, you could just
> start the thread and let it go.  Sometimes there is no need to make
> the user wait until it is done.

Until the process is done (the info is retrieved) there is nothing for
the user to do.

> BTW, you are aware that Tk is not thread-safe, right?  It is only safe
> to modify any widgets in the Tk main thread.

Which is why I asked the question. The long running process is in a
class that has no need to know that it's being run from a gui so I don't
want to put any gui stuff in it yet I need to update the gui while it's
running. By putting it in a thread I can stay in the main program to
update the gui and then proceed when the thread finishes.

This is working fine for me. I've got a widget with an after() method
that continuously checks a global variable that is written to (within
a lock) by the thread. The thread itself doesn't make any actual gui
calls, it just appends to the global list variable.

BTW, I'm calling my program PyNewzBin - The Multi-Part Binary Usenet
File Retriever. It's purpose is to more easily deal with newsgroups
like alt.binaries.mp3.audiobooks than a regular newsreader does. It's
most useful on a broadband connection since you can use it for the long
downloads and read news with your regular newsreader at the same time.

If anyone on this list thinks such a program would be useful to them and
would like to offer suggestions for improving it, I'd be glad to send
them the program.

Each of us certainly gets from Nature, from God, the right to
defend his person, his liberty, and his property, since they are
the three elements constituting or sustaining life, elements which
are mutually complementary and which cannot be understood without
one another. For what are our faculties, if not an extension of
our personality, and what is property, if not an extension of our
	-- Frédéric Bastiat (1801-1850)
    Rick Pasotto    rickp@telocity.com    http://www.niof.net