THREADS use 100 % CPU all the time

M

matthiasjanes

Hi all,

I have a application where I use different threads. actually all is
working - BUT I just discovered that the 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 http://www.webwareforpython.org/TaskKit/Docs/Source/ Docs/TaskKit.Scheduler.html
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


Does anyone know how to run this without consuming all CPU.

regards,

MJ
 
A

A.B., Khalid

Hi all,

I have a application where I use different threads. actually all is
working - BUT I just discovered that the 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 http://www.webwareforpython.org/TaskKit/Docs/Source/ Docs/TaskKit.Scheduler.html
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

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
 
M

matthiasjanes

I have a application where I use different threads. actually all is
working - BUT I just discovered that the 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 http://www.webwareforpython.org/TaskKit/Docs/Source/ Docs/TaskKit.Scheduler.html
but I checked and a very simple exampe with threading gives me also
all the time 100% CPU.
Code:
[/QUOTE]

import threading, time[/QUOTE]
[QUOTE]
class TestThread ( threading.Thread ):
def run ( self ):
print 'TEST'[/QUOTE]
[QUOTE]
t = TestThread()
t.start()[/QUOTE]
[QUOTE]
while (True):
pass [QUOTE]

Does anyone know how to run this without consuming all CPU.

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




Thanks a lot both of you.

MJ
 
G

Gabriel Genellina

On Apr 11, 2:38 am, (e-mail address removed) wrote:
I have a application where I use different threads. actually all is
working - BUT I just discovered that the CPU is always 100 % [/
b]used.

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

t = TestThread()
t.start()

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


If all you want is to wait until the thread finishes, use t.join() instead
of that infinite loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top