0

I want to call a function on the main thread loop every N seconds.

To do this, a Threading.Timer is initialized with a _timer_handler() function. This function is called in the Timer thread context, I try to call _refresh_token() on the main thread loop but it's never called.

# coding: utf-8
import asyncio
import datetime
import time
import threading

def thread_msg(msg):
    print("{} - Current Thread is {} | {} |".format(datetime.datetime.now(), threading.current_thread().ident, threading.current_thread().name), msg)

class My_Session():

    def __init__(self, delay=30):
        self._timer = None
        self._delay = delay
        self._loop = asyncio.get_event_loop()

    def start(self):
        thread_msg("Start session")
        self._init_timer()

    def stop(self):
        if self._timer:
            self._timer.cancel()
        thread_msg("Stop session")

    def _init_timer(self):
        self._timer = threading.Timer(self._delay, self._timer_handler)
        print("Set timer to {} seconds to refresh token".format(self._delay))
        self._timer.start()

    def _timer_handler(self):
        try:
            thread_msg("Time out, call self._refresh_token")
            self._loop.call_soon_threadsafe(self._refresh_token)
            thread_msg("call self._refresh_token done")
        except Exception as e:
            thread_msg("Exception in _timer_handler: {}".format(e))

    def _refresh_token(self):
        thread_msg("_refresh_token is called properly,\n   reinitialize the timer...")
        self._init_timer()

if __name__ == '__main__':
    thread_msg("Start script")
    session = My_Session(5)
    session.start()
    try:
        while True:
            thread_msg("loop")
            time.sleep(1)
    except KeyboardInterrupt:
        session.stop()

Output is :

2019-06-24 20:21:29.776809 - Current Thread is 10220 | MainThread | Start script
2019-06-24 20:21:29.777906 - Current Thread is 10220 | MainThread | Start session
Set timer to 5 seconds to refresh token
2019-06-24 20:21:29.778310 - Current Thread is 10220 | MainThread | loop
2019-06-24 20:21:30.787545 - Current Thread is 10220 | MainThread | loop
2019-06-24 20:21:31.791333 - Current Thread is 10220 | MainThread | loop
2019-06-24 20:21:32.794627 - Current Thread is 10220 | MainThread | loop
2019-06-24 20:21:33.806744 - Current Thread is 10220 | MainThread | loop
2019-06-24 20:21:34.790288 - Current Thread is 17148 | Thread-1 | Time out, call self._refresh_token
2019-06-24 20:21:34.790573 - Current Thread is 17148 | Thread-1 | call self._refresh_token done
2019-06-24 20:21:34.820744 - Current Thread is 10220 | MainThread | loop
2019-06-24 20:21:35.822738 - Current Thread is 10220 | MainThread | loop
2019-06-24 20:21:36.830907 - Current Thread is 10220 | MainThread | loop
2019-06-24 20:21:37.833233 - Current Thread is 10220 | MainThread | loop
Moonstone
  • 1
  • 2

1 Answers1

0

A solution : replace time.sleep(1) with asyncio.get_event_loop().run_until_complete(asyncio.sleep(1))

Then, output is:

2019-07-07 23:25:26.128351 - Current Thread is 49704 | MainThread | Start script
2019-07-07 23:25:26.129315 - Current Thread is 49704 | MainThread | Start session
Set timer to 5 seconds to refresh token
2019-07-07 23:25:26.130314 - Current Thread is 49704 | MainThread | loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
2019-07-07 23:25:28.131111 - Current Thread is 49704 | MainThread | loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
2019-07-07 23:25:30.131815 - Current Thread is 49704 | MainThread | loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
2019-07-07 23:25:31.130414 - Current Thread is 17000 | Thread-1 | Time out, call self._refresh_token
2019-07-07 23:25:31.130414 - Current Thread is 17000 | Thread-1 | call self._refresh_token done2019-07-07 23:25:31.130414 - Current Thread is 49704 | MainThread    
_refresh_token is called properly,
reinitialize the timer...
Set timer to 5 seconds to refresh token
2019-07-07 23:25:32.132879 - Current Thread is 49704 | MainThread | loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
2019-07-07 23:25:34.134277 - Current Thread is 49704 | MainThread | loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
2019-07-07 23:25:36.131731 - Current Thread is 19988 | Thread-2 | Time out, call self._refresh_token
2019-07-07 23:25:36.132455 - Current Thread is 19988 | Thread-2 | call self._refresh_token done
2019-07-07 23:25:36.132455 - Current Thread is 49704 | MainThread | _refresh_token is called properly,
reinitialize the timer...
Set timer to 5 seconds to refresh token
2019-07-07 23:25:36.133478 - Current Thread is 49704 | MainThread | loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
2019-07-07 23:25:38.134883 - Current Thread is 49704 | MainThread | loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
2019-07-07 23:25:40.135312 - Current Thread is 49704 | MainThread | loop <_WindowsSelectorEventLoop running=False closed=False debug=False>
2019-07-07 23:25:41.133663 - Current Thread is 25112 | Thread-3 | Time out, call self._refresh_token
2019-07-07 23:25:41.133663 - Current Thread is 25112 | Thread-3 |2019-07-07     
23:25:41.133663 - Current Thread is 49704 | MainThread |  call self._refresh_token done
_refresh_token is called properly,
reinitialize the timer...
Set timer to 5 seconds to refresh token
...
Moonstone
  • 1
  • 2