<s> import logging <EOL> from datetime import datetime <EOL> from rx . disposables import Disposable , SingleAssignmentDisposable , CompositeDisposable <EOL> from rx . concurrency . scheduler import Scheduler <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> class IOLoopScheduler ( Scheduler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , loop = None ) : <EOL> from tornado import ioloop <EOL> self . loop = loop or ioloop . IOLoop . current ( ) <EOL> def schedule ( self , action , state = None ) : <EOL> """<STR_LIT>""" <EOL> scheduler = self <EOL> disposable = SingleAssignmentDisposable ( ) <EOL> disposed = [ False ] <EOL> def interval ( ) : <EOL> if not disposed [ <NUM_LIT:0> ] : <EOL> disposable . disposable = action ( scheduler , state ) <EOL> self . loop . add_callback ( interval ) <EOL> def dispose ( ) : <EOL> disposed [ <NUM_LIT:0> ] = True <EOL> return CompositeDisposable ( disposable , Disposable ( dispose ) ) <EOL> def schedule_relative ( self , duetime , action , state = None ) : <EOL> """<STR_LIT>""" <EOL> scheduler = self <EOL> seconds = scheduler . to_relative ( duetime ) / <NUM_LIT> <EOL> if not seconds : <EOL> return scheduler . schedule ( action , state ) <EOL> disposable = SingleAssignmentDisposable ( ) <EOL> def interval ( ) : <EOL> disposable . disposable = action ( scheduler , state ) <EOL> log . debug ( "<STR_LIT>" , seconds ) <EOL> handle = self . loop . call_later ( seconds , interval ) <EOL> def dispose ( ) : <EOL> self . loop . remove_timeout ( handle ) <EOL> return CompositeDisposable ( disposable , Disposable ( dispose ) ) <EOL> def schedule_absolute ( self , duetime , action , state = None ) : <EOL> """<STR_LIT>""" <EOL> duetime = self . to_datetime ( duetime ) <EOL> return self . schedule_relative ( duetime - self . now ( ) , action , state ) <EOL> def now ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . to_datetime ( self . loop . time ( ) ) </s>
