THREADS use 100 % CPU all the time

Discussion in 'Python' started by matthiasjanes@gmx.net, Apr 11, 2007.

  1. Guest

    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
     
    , Apr 11, 2007
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Thomas_Kr=FCger?=, Apr 11, 2007
    #2
    1. Advertising

  3. A.B., Khalid Guest

    On Apr 11, 2:38 am, wrote:
    > 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
     
    A.B., Khalid, Apr 11, 2007
    #3
  4. Guest

    On Apr 11, 1:36 pm, "A.B., Khalid" <> wrote:
    > On Apr 11, 2:38 am, wrote:
    >
    >
    >
    > > 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:
    [/color]
    >[color=green]
    > > import threading, time[/color]
    >[color=green]
    > > class TestThread ( threading.Thread ):
    > > def run ( self ):
    > >     print 'TEST'[/color]
    >[color=green]
    > > t = TestThread()
    > > t.start()[/color]
    >[color=green]
    > > while (True):
    > >     pass[/color]
    >[color=green]
    > > 

    >
    > > 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, 2007
    #4
  5. En Wed, 11 Apr 2007 08:36:57 -0300, A.B., Khalid <>
    escribió:

    > On Apr 11, 2:38 am, 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.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Apr 11, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jrbrady

    Web Server Using 100% CPU Time

    jrbrady, Apr 19, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    135
    jrbrady
    Apr 19, 2006
  2. Ed McNierney

    ServerXMLHTTP uses 100% CPU for a long time

    Ed McNierney, Dec 2, 2005, in forum: ASP General
    Replies:
    7
    Views:
    215
    Bob Barrows [MVP]
    Dec 3, 2005
  3. Yakov

    100% cpu loop of time() ...

    Yakov, Nov 15, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    117
    Yakov
    Nov 21, 2007
  4. fred
    Replies:
    3
    Views:
    291
    Zifud
    Mar 17, 2005
  5. Replies:
    5
    Views:
    902
Loading...

Share This Page