<s> import asyncore <EOL> import time <EOL> import functools <EOL> from monocle import launch <EOL> class EventLoop ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _running = True <EOL> self . _queue = [ ] <EOL> self . _map = { } <EOL> def queue_task ( self , delay , callable , * args , ** kw ) : <EOL> now = time . time ( ) <EOL> when = now + delay <EOL> self . _queue . append ( ( when , callable , args , kw ) ) <EOL> self . _queue . sort ( reverse = True ) <EOL> def run ( self ) : <EOL> while self . _running : <EOL> timeout = <NUM_LIT:0> <EOL> if self . _queue : <EOL> next = self . _queue [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] - time . time ( ) <EOL> if next <= <NUM_LIT:0> : <EOL> task = self . _queue . pop ( ) <EOL> launch ( task [ <NUM_LIT:1> ] , * task [ <NUM_LIT:2> ] , ** task [ <NUM_LIT:3> ] ) <EOL> else : <EOL> timeout = next <EOL> if self . _map : <EOL> asyncore . loop ( timeout = timeout , use_poll = True , count = <NUM_LIT:1> , <EOL> map = self . _map ) <EOL> else : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> def halt ( self ) : <EOL> self . _running = False <EOL> evlp = EventLoop ( ) <EOL> queue_task = evlp . queue_task <EOL> run = evlp . run <EOL> halt = evlp . halt </s>
