'Create handlers used to implement time limits.'

def _create_timelimit_handlers(self, hub):

    call_later = hub.call_later
    trefs = self._tref_for_id = WeakValueDictionary()
    def on_timeout_set(R, soft, hard):
        if soft:
            trefs[R._job] = call_later(soft, self._on_soft_timeout, R._job, soft, hard, hub)
        elif hard:
            trefs[R._job] = call_later(hard, self._on_hard_timeout, R._job)
    self.on_timeout_set = on_timeout_set
    def _discard_tref(job):
        try:
            tref = trefs.pop(job)
            tref.cancel()
            del tref
        except (KeyError, AttributeError):
            pass
    self._discard_tref = _discard_tref
    def on_timeout_cancel(R):
        _discard_tref(R._job)
    self.on_timeout_cancel = on_timeout_cancel
