'Cancel the task and mark it as done.'

def cancel(self, grace_period=None):

    if self.done():
        return
    if ((not self.started()) or (grace_period is None)):
        LOG.debug('%s    cancelled', six.text_type(self))
        self._done = True
        if self.started():
            self._runner.close()
    else:
        timeout = TimedCancel(self, grace_period)
        if ((self._timeout is None) or (timeout < self._timeout)):
            self._timeout = timeout
