Author SD
Recipients SD
Date 2020-07-14.22:15:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1594764957.86.0.842448865524.issue41299@roundup.psfhosted.org>
In-reply-to
Content
The overhead in Python 3 for threading.Event().wait() is much larger than Python 2. I am trying to run a script at 60hz which worked correctly in Python2 but does not in Python 3. Here is a minimal example to demonstrate: 

#!/usr/bin/env python

import threading
import time

def sample_thread(stop_ev):
    while not stop_ev.is_set():
        t2 = time.time() 
        stop_ev.wait(0.016999959945)
        print((time.time() - t2))

def main():
    stop_ev = threading.Event()
    sample_t = threading.Thread(target=sample_thread, args=(stop_ev, ))
    sample_t.start()

    # Other stuff here, sleep is just dummy
    time.sleep(14)

    stop_ev.set()

    print('End reached.')

if __name__ == '__main__':
    main()

Python 2.7.0 consistently prints :

0.0169999599457
0.0169999599457
0.0170001983643
0.0169999599457
0.0169999599457
0.0169999599457
0.0169999599457
0.0169999599457

Python 3.8.2 waits much longer

0.031026363372802734
0.0320279598236084
0.031026363372802734
0.031026840209960938
0.031527042388916016
0.031026601791381836
0.03103041648864746
0.03302431106567383
History
Date User Action Args
2020-07-14 22:15:57SDsetrecipients: + SD
2020-07-14 22:15:57SDsetmessageid: <1594764957.86.0.842448865524.issue41299@roundup.psfhosted.org>
2020-07-14 22:15:57SDlinkissue41299 messages
2020-07-14 22:15:57SDcreate