1
from apscheduler.scheduler import Scheduler
def req():
    print 'some thing like hello world or foo'


if __name__ == '__main__':
    scheduler = Scheduler()
    scheduler.add_date_job(req, datetime(2014, 1, 6, 21, 40, 00))
    scheduler.start()
while True:
      pass

i try above code

run it in python2.7 and cpu usage going up to 98% it is normal?! or something wrong with apscheduler package,

can improve it with more cores? please let me know about cpu usage in python thanks

Core of system 1

1
  • 1
    while True: pass That is going to use up your CPU
    – Aida Paul
    Commented Jan 6, 2014 at 11:12

3 Answers 3

4

while True: pass

That is going to use up your CPU. I understand that you run it to check whether the event will fire? Then you may want to utilize sleep inside of your loop to lower the times code is executed, for example.

from apscheduler.scheduler import Scheduler
import time
def req():
    print 'some thing like hello world or foo'


if __name__ == '__main__':
    scheduler = Scheduler()
    scheduler.add_date_job(req, datetime(2014, 1, 6, 21, 40, 00))
    scheduler.start()
while True:
      time.sleep(1)
      pass
5
  • work like a charm :D thanks but i dont understand what to do time.sleep(1)? Commented Jan 6, 2014 at 11:21
  • @daviddavoodi time.sleep freezes the execution of your software for precisely 1 second. You can read up on it in the manual docs.python.org/2/library/time.html. Also if my solution resolved your problem, please mark it as the answer.
    – Aida Paul
    Commented Jan 6, 2014 at 11:22
  • Yes but after 5 min :) Commented Jan 6, 2014 at 11:23
  • sorry dude Is this a second reveals the influence of the original code? i mean when req method fire? in datetime(2014, 1, 6, 21, 40, 00) or datetime(2014, 1, 6, 21, 40, 01) ? Commented Jan 6, 2014 at 13:54
  • @daviddavoodi dunno as i never toyed with appschedule. My solution is supposed to point you into right direction, the fine print (for example how long the sleep should actually be, second, 10th of a second?) are for you to figure out.
    – Aida Paul
    Commented Jan 6, 2014 at 13:58
2

remove

while True:
      pass

that causes high cpu usage.

0
2

This piece of code

while True:
      pass

Is doing an active infinite loop: there is no input, output or system calls in general. This means that all CPU available is used by that loop. Use a sleep, for instance, or launch a non daemon thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.