List Info

Thread: Threads in wx




Threads in wx
user name
2007-11-06 00:12:25
Everything I've read about threading usually starts off
with

"Don't use the thread module, use threading
instead."

"calls to sleep() block", (don't call sleep when
using python threads)

When I looked at the Thread demo for wx, I was surprised to
see
import thread
There are also calls to sleep in the wx  thread demo.

This demo is doing just what the python multi-threading
resources
state not do do,
but I don't understand why.

------------------------------------------------------------
---------
To unsubscribe, e-mail: wxPython-users-unsubscribelists.wxwidgets.org
For additional commands, e-mail: wxPython-users-helplists.wxwidgets.org


Re: Re: Threads in wx
country flaguser name
United States
2007-11-06 11:35:12
Bryan Cole wrote:
> On Mon, 2007-11-05 at 22:12 -0800, Tony Cappellini
wrote:

>> "calls to sleep() block", (don't call
sleep when using python threads)
> 
> Well by definition, calls to sleep *should* block the
calling thread!
> However, sleep also releases the global interpreter
lock, allowing other
> threads to run while the sleeping thread err.. sleeps.
I can't see any
> reason why sleep() and threads are incompatible.

And specifically in the case of the demo, a blocking sleep
is exactly 
what is wanted.  Other threading situations will have
different needs.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java
give you jitters?  Relax with wxPython!


------------------------------------------------------------
---------
To unsubscribe, e-mail: wxPython-users-unsubscribelists.wxwidgets.org
For additional commands, e-mail: wxPython-users-helplists.wxwidgets.org


Re: Threads in wx
user name
2007-11-06 14:04:36
On Nov 6, 2007 12:12 AM, Tony Cappellini <cappy2112gmail.com> wrote:
> Everything I've read about threading usually starts off
with
>
> "Don't use the thread module, use threading
instead."
>
> "calls to sleep() block", (don't call sleep
when using python threads)
>

I've actually never seen this last advice. You're probably
misreading
reactions to the incorrect use of sleep into a prohibition
on its use.

For example, many naive programmers will write something
like the following:

def doSomething():
    t = Thread(target=doSomethingElse)
    t.start()
    sleep(4) #doSomethingElse takes 4 seconds to finish
    ... [code that assumes doSomethignElse is complete]...



There are relatively few uses cases for a sleep in the real
world,
which is why you don't see it much. By far (in my
experience) the most
common use is actually in threading demos, where it's used
to simulate
a long-running task. The second most common is to give up
your time
slice when you busy-loop:

while True:
    if someFlagIsSet():
        doSomething()

will eat 100% of cpu, and by adding a sleep to the loop you
can cut
down on the CPU usage. Busy loop polling like this is
frowned upon -
in the example above you'd block on a semaphore or event
instead - but
sometimes it's unavoidable and when it is sleep() is exactly
the right
thing to use.

So the prohibition isn't (or shouldn't) be on sleep so much
as that
most use of sleep (by beginners) is a case when they really
should be
waiting on an event or joining a thread.

------------------------------------------------------------
---------
To unsubscribe, e-mail: wxPython-users-unsubscribelists.wxwidgets.org
For additional commands, e-mail: wxPython-users-helplists.wxwidgets.org


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )