THREADS use 100 % CPU all the time

A.B., Khalid khabkr at yahoo.com
Wed Apr 11 07:36:57 EDT 2007


On Apr 11, 2:38 am, matthiasja... at gmx.net wrote:
> Hi all,
>
> I have a application where I use different threads. actually all is
> working - BUT I just discovered that the [b]CPU is always 100 % [/
> b]used.
>
> on the 32-bit machine athlon XP, as well as on the amd 64-bit AMD
> Athlon(TM) 64 X2 Dual-Core.
>
> I have to admit I'm not used to threads. I actually use a thirdparty
> scheduler [url]http://www.webwareforpython.org/TaskKit/Docs/Source/
> Docs/TaskKit.Scheduler.html[/url]
> but I checked and a very simple exampe with threading gives me also
> all the time 100% CPU.
>
> [code]
>
> import threading, time
>
> class TestThread ( threading.Thread ):
> def run ( self ):
>     print 'TEST'
>
> t = TestThread()
> t.start()
>
> while (True):
>     pass
>
> [/code]
>
> Does anyone know how to run this without consuming all CPU.
>
> regards,
>
> MJ


You need your program to sleep a while to allow a switch to other
tasks. Like so:

###
import threading, time

class TestThread(threading.Thread):
    def run(self):
        print 'TEST'

t = TestThread()
t.start()

while (True):
    time.sleep(0.01)
    pass
###


Regards




More information about the Python-list mailing list