473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

THREADS use 100 % CPU all the time

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 http://www.webwareforpython.org/Task...Scheduler.html
but I checked and a very simple exampe with threading gives me also
all the time 100% CPU.
Expand|Select|Wrap|Line Numbers
  1.  
  2. import threading, time
  3.  
  4. class TestThread ( threading.Thread ):
  5. def run ( self ):
  6. print 'TEST'
  7.  
  8. t = TestThread()
  9. t.start()
  10.  
  11.  
  12. while (True):
  13. pass
  14.  
  15.  

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

regards,

MJ

Apr 11 '07 #1
4 9403
ma***********@gmx.net schrieb:
while (True):
pass
Does anyone know how to run this without consuming all CPU.
Your while loop is taking all the CPU time.

Thomas

--
sinature: http://nospam.nowire.org/signature_usenet.png
Apr 11 '07 #2
On Apr 11, 2:38 am, matthiasja...@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 http://www.webwareforpython.org/Task...Scheduler.html
but I checked and a very simple exampe with threading gives me also
all the time 100% CPU.

Expand|Select|Wrap|Line Numbers
  1. import threading, time
  2. class TestThread ( threading.Thread ):
  3. def run ( self ):
  4.     print 'TEST'
  5. t = TestThread()
  6. t.start()
  7. while (True):
  8.     pass
  9.  

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

Apr 11 '07 #3
On Apr 11, 1:36 pm, "A.B., Khalid" <kha...@yahoo.comwrote:
On Apr 11, 2:38 am, matthiasja...@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 http://www.webwareforpython.org/Task...Scheduler.html
but I checked and a very simple exampe with threading gives me also
all the time 100% CPU.
Expand|Select|Wrap|Line Numbers
  1.         
  2.                  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

    Thanks a lot both of you.

    MJ

    Apr 11 '07 #4
    En Wed, 11 Apr 2007 08:36:57 -0300, A.B., Khalid <kh****@yahoo.com>
    escribió:
    On Apr 11, 2:38 am, matthiasja...@gmx.net wrote:
    >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.
    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.

    --
    Gabriel Genellina
    Apr 11 '07 #5

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    1
    by: Huzefa | last post by:
    I am working on a amll project in Java that includes many classes. Each of the classes has a Logger object. I have associated a FileHandler with each of these Logger objects. The file is the same...
    3
    by: Ronan Viernes | last post by:
    Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
    7
    by: Mr. Mountain | last post by:
    In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long...
    10
    by: [Yosi] | last post by:
    I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes,...
    6
    by: RahimAsif | last post by:
    Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
    11
    by: mareal | last post by:
    I am trying to write a basic load balancer (in our web service) solution. The purpose of this load balancer is to keep an array updated with server status. We have several servers that can be...
    10
    by: cj | last post by:
    I'm writing a TCP/IP server app that will have many simultaneous connections. The main thread listens for new connections and starts a thread to handle each requested connection. These are short...
    2
    by: jgbid | last post by:
    Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
    18
    by: Jon Slaughter | last post by:
    "Instead of just waiting for its time slice to expire, a thread can block each time it initiates a time-consuming activity in another thread until the activity finishes. This is better than...
    4
    by: tdahsu | last post by:
    All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
    0
    by: DolphinDB | last post by:
    The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
    0
    by: DolphinDB | last post by:
    Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
    0
    by: ryjfgjl | last post by:
    ExcelToDatabase: batch import excel into database automatically...
    0
    by: Vimpel783 | last post by:
    Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
    0
    by: jfyes | last post by:
    As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
    0
    by: ArrayDB | last post by:
    The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
    0
    by: Defcon1945 | last post by:
    I'm trying to learn Python using Pycharm but import shutil doesn't work
    0
    by: Shællîpôpï 09 | last post by:
    If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
    0
    by: af34tf | last post by:
    Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.