THREADS use 100 % CPU all the time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • matthiasjanes@gmx.net

    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.


    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

  • =?ISO-8859-1?Q?Thomas_Kr=FCger?=

    #2
    Re: THREADS use 100 % CPU all the time

    matthiasjanes@g mx.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

    Comment

    • A.B., Khalid

      #3
      Re: THREADS use 100 % CPU all the time

      On Apr 11, 2:38 am, matthiasja...@g mx.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.
      >
      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(thre ading.Thread):
      def run(self):
      print 'TEST'

      t = TestThread()
      t.start()

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


      Regards

      Comment

      • matthiasjanes@gmx.net

        #4
        Re: THREADS use 100 % CPU all the time

        On Apr 11, 1:36 pm, "A.B., Khalid" <kha...@yahoo.c omwrote:
        Code:
        >[QUOTE]
         import threading, time
        On Apr 11, 2:38 am, matthiasja...@g mx.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.
        >
        >[QUOTE] class TestThread ( threading.Thread ): def run ( self ): print 'TEST'[/QUOTE] >[QUOTE] t = TestThread() t.start()[/QUOTE] >[QUOTE] while (True): pass[/QUOTE] >[QUOTE] [/QUOTE]
        >
        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(thre ading.Thread):
        def run(self):
        print 'TEST'
        >
        t = TestThread()
        t.start()
        >
        while (True):
        time.sleep(0.01 )
        pass
        ###
        >
        Regards[/QUOTE]



        Thanks a lot both of you.

        MJ

        Comment

        • Gabriel Genellina

          #5
          Re: THREADS use 100 % CPU all the time

          En Wed, 11 Apr 2007 08:36:57 -0300, A.B., Khalid <khabkr@yahoo.c om>
          escribió:
          On Apr 11, 2:38 am, matthiasja...@g mx.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

          Comment

          Working...