This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python 3.6 on Windows wastes a lot of CPU cycles in a while loop
Type: resource usage Stage: resolved
Components: Windows Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: paul.moore, prahladyeri, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords:

Created on 2017-01-05 23:28 by prahladyeri, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Python_high_CPU_Windows.png prahladyeri, 2017-01-05 23:28
Messages (3)
msg284787 - (view) Author: Prahlad Yeri (prahladyeri) Date: 2017-01-05 23:28
I'm running Python 3.6 on Windows 7. It wastes a lot of cpu cycles when inside a loop. In attached screenshot, you'll find that ~25% cpu is used by Python, while all I'm doing is running a pomodoro script that waits for a specific timeslot to complete. Here is the code for pomodoro.py that runs the while loop inside start_tracking() function:

https://github.com/prahladyeri/PyPomodoro/blob/master/pomodoro.py



def start_tracking(task):
	global last_beep
	print("Working on %s:%s (%d minutes)." % (task['category'], task['name'], task['duration']))
	print("Started tracking at %s." % (datetime.datetime.now().strftime("%H:%M")))
	print("Beep interval is set to %d minutes." % config.slot_interval)
	session_start_time = datetime.datetime.now()
	task_end_time = session_start_time + datetime.timedelta(minutes=task['duration'])
	last_beep = datetime.datetime.now()
	notified = False
	while(True):
		now = datetime.datetime.now()
		diff = (now - last_beep).total_seconds() / 60.0 #in minutes
		minutes_worked = round(diff)
		reminder_text = "%s:%s" % (task['category'], task['name'])
		#diff = diff.total_seconds() / (60.0) 
		if (diff >= config.slot_interval): #30
			#notify
msg284788 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-01-05 23:32
The code doesn't seem like a bug in Python, but more a classic inefficient busy loop pattern. Your loop doesn't sleep and so eats all the CPU.

I suggest to close the issue and ask Python questions on a place to ask questions, not on the Python *bug tracker*.
msg284789 - (view) Author: Prahlad Yeri (prahladyeri) Date: 2017-01-05 23:43
Hi STINNER Victor,

Thanks a lot, after adding the sleep function, it has stopped wasting the CPU cycles! A long while ago, I remember coding a similar loop in linux and not faced such issue, so I thought maybe it was a bug in 3.6.

Anyway, I'm closing this.
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73359
2017-01-06 00:16:04eryksunsetstage: resolved
2017-01-05 23:43:52prahladyerisetstatus: open -> closed
resolution: not a bug
messages: + msg284789
2017-01-05 23:32:50vstinnersetnosy: + vstinner
messages: + msg284788
2017-01-05 23:28:45prahladyericreate