classification
Title: heapq item comparison problematic with sched's events
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: kfj, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2009-04-24 13:35 by kfj, last changed 2016-11-22 06:02 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
test_sched_noncomparable_args.patch serhiy.storchaka, 2016-10-25 09:11 review
Messages (9)
msg86408 - (view) Author: Kay F. Jahnke (kfj) Date: 2009-04-24 13:35
scheduler uses heapq to schedule it's events. Heapq uses plain >/< 
comparisons on the events. Now that comparisons of incomparable data 
are no longer valid, the comparison fails if two events are scheduled 
for the same time with the same priority, since the comparison 
continues with comparing the 'action' components ov the event's tuple. 
I suppose this behaviour is unwanted - at least I did not expect it and 
it took me some time to figure out what it was due to. I worked around 
it by assigning comparison functions to the event data type - since 
this is part of the standard library, maybe a general fix should be 
considered? Here's my humble snippet of code:

def evtlt ( self , other ):
	if self.time < other.time:
		return True
	elif self.time == other.time:
		return self.priority < other.priority
	return False

sched.Event.__lt__ = evtlt

def evtgt ( self , other ):
	if self.time > other.time:
		return True
	elif self.time == other.time:
		return self.priority > other.priority
	return False

sched.Event.__gt__ = evtgt

If anyone would care to reproduce the (?)bug, try:

import sched

def foo():
	pass

def bar():
	pass

s = sched.scheduler(None, None) 

s.enterabs ( 0 , 0 , foo , () )
s.enterabs ( 0 , 0 , bar , () )

this produces the following output:

Traceback (most recent call last):
  File "./schedbug.py", line 12, in <module>
    s.enterabs ( 0 , 0 , bar , () )
  File "c:\Programme\Python3.0\lib\sched.py", line 54, in enterabs
    heapq.heappush(self._queue, event)
TypeError: unorderable types: function() < function()
msg86424 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-04-24 18:49
Fixed r71844 and 71845.
msg278058 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-04 16:18
Needed tests for this feature. Otherwise sched.Event can be "enhanced" by removing ordering methods (issue28330).
msg279308 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-10-24 14:32
New changeset ee476248a74a by Raymond Hettinger in branch '3.6':
Issue #5830:  Remove old comment.  Add empty slots.
https://hg.python.org/cpython/rev/ee476248a74a
msg279379 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-25 09:11
Here is a test.
msg280670 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-12 20:14
Ping.
msg281419 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-11-22 00:48
New changeset ecc6f7940e02 by Raymond Hettinger in branch '3.6':
Issue #5830:  Add test for ee476248a74a.  (Contributed by Serhiy Storchaka.)
https://hg.python.org/cpython/rev/ecc6f7940e02
msg281420 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-11-22 00:49
Thanks Serhiy.

FWIW, I changed the variable name from "l" to "seq" for readability.
msg281435 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-22 06:02
Agreed, "seq" is better name.

Thanks Raymond.
History
Date User Action Args
2016-11-22 06:02:53serhiy.storchakasetstage: resolved
2016-11-22 06:02:40serhiy.storchakasetmessages: + msg281435
2016-11-22 00:49:39rhettingersetstatus: open -> closed

messages: + msg281420
2016-11-22 00:48:33python-devsetmessages: + msg281419
2016-11-12 20:14:21serhiy.storchakasetmessages: + msg280670
2016-10-25 09:11:49serhiy.storchakasetfiles: + test_sched_noncomparable_args.patch
2016-10-25 09:11:38serhiy.storchakasetfiles: - test_sched_noncomparable_args.patch
2016-10-25 09:11:04serhiy.storchakasetfiles: + test_sched_noncomparable_args.patch
keywords: + patch
messages: + msg279379

versions: + Python 3.5, Python 3.6, Python 3.7, - Python 3.0, Python 3.1
2016-10-24 14:32:42python-devsetnosy: + python-dev
messages: + msg279308
2016-10-23 20:20:48serhiy.storchakasetstatus: closed -> open
2016-10-04 16:18:30serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg278058
2009-04-24 18:49:52rhettingersetstatus: open -> closed

assignee: rhettinger
versions: + Python 3.1
nosy: + rhettinger

messages: + msg86424
resolution: fixed
2009-04-24 13:35:04kfjcreate