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: Event scheduler page example
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: cheryl.sabella, docs@python, giampaolo.rodola, mediator42, xtreak
Priority: normal Keywords: patch

Created on 2018-09-14 12:17 by mediator42, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12659 closed python-dev, 2019-04-02 09:39
Messages (3)
msg325345 - (view) Author: mediator42 (mediator42) Date: 2018-09-14 12:17
Hi,
I am pretty confident, that the example you put on the Event scheduler page (https://docs.python.org/3.7/library/sched.html) is somehow strange. 

If the priority argument behave as described, it should print the "keyword" line before the "positional" line, since these events are scheduled for the same time but "keyword" has a higher priority than "positional".

When I run this script on my PC it behaves correctly.

Best reagrds
msg339330 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-04-02 11:33
enter takes a relative time and priority is taken into consideration only when time is equal. So in the example enter(0.001, 2) is executed first and there is some delay in executing enter(0.001, 2). You can also view the queue with s.queue which is a heap. So even though they look similar the first call is scheduled earlier than the second one even with lower priority. 

Your report is correct if the example used enterabs where the time is absolute and then the two events are ordered based on priority with keyword executed first in the heapq used . In your script you can print s.sched and maybe add the same to the report?

# enter and s.sched prints different time with positional scheduled to be executed first in time.

➜  cpython git:(master) cat /tmp/bar.py
import sched, time
s = sched.scheduler(time.time, time.sleep)

def print_time(a='default'):
    print("From print_time", time.time(), a)

def print_some_times():
    print(time.time())
    s.enter(0.001, 2, print_time, argument=('positional',))
    s.enter(0.001, 1, print_time, kwargs={'a': 'keyword'})
    print(s.queue)
    s.run()
    print(time.time())

print_some_times()
➜  cpython git:(master) ./python.exe /tmp/bar.py
1554204002.401897
[Event(time=1554204002.40309, priority=2, action=<function print_time at 0x10e9747e0>, argument=('positional',), kwargs={}), Event(time=1554204002.403158, priority=1, action=<function print_time at 0x10e9747e0>, argument=(), kwargs={'a': 'keyword'})]
From print_time 1554204002.40331 positional
From print_time 1554204002.403441 keyword
1554204002.403517

# enterabs and s.sched prints same time with keyword ordered first based on priority with time being equal.

➜  cpython git:(master) cat /tmp/baz.py
import sched, time
s = sched.scheduler(time.time, time.sleep)

def print_time(a='default'):
    print("From print_time", time.time(), a)

def print_some_times():
    print(time.time())
    s.enterabs(0.001, 2, print_time, argument=('positional',))
    s.enterabs(0.001, 1, print_time, kwargs={'a': 'keyword'})
    print(s.queue)
    s.run()
    print(time.time())

print_some_times()
➜  cpython git:(master) ./python.exe /tmp/baz.py
1554204136.854676
[Event(time=0.001, priority=1, action=<function print_time at 0x1042767e0>, argument=(), kwargs={'a': 'keyword'}), Event(time=0.001, priority=2, action=<function print_time at 0x1042767e0>, argument=('positional',), kwargs={})]
From print_time 1554204136.855422 keyword
From print_time 1554204136.855669 positional
1554204136.855788
msg345126 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-06-10 11:48
Thank you for the report.  As others have said, the actual time.time() value for the two `s.enter` events is different even though the `delay` value of 5 seems like it should be the same.  As @xtreak pointed out, the example is clever in showing that using `enter` instead of `enterabs` can make the queue behave in unexpected ways.

Closing as 'not a bug'.
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78858
2019-06-10 11:48:23cheryl.sabellasetstatus: open -> closed

nosy: + cheryl.sabella
messages: + msg345126

resolution: not a bug
stage: patch review -> resolved
2019-04-02 11:33:30xtreaksetnosy: + xtreak, giampaolo.rodola
messages: + msg339330
2019-04-02 09:39:08python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request12588
2018-09-14 12:17:49mediator42create