On Nov 6, 2007 12:12 AM, Tony Cappellini <cappy2112 gmail.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-unsubscribe lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help lists.wxwidgets.org
|