<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def cancel(self, nids=None):
 """ Cancel all the tasks that are in the queue. nids is an optional list of node identifiers used to filter the tasks. Returns: Number of jobs cancelled, negative value if error """

        if self.has_chrooted:
            # TODO: Use paramiko to kill the job?
            warnings.warn("Cannot cancel the flow via sshfs!")
            return -1

        # If we are running with the scheduler, we must send a SIGKILL signal.
        if os.path.exists(self.pid_file):
            cprint("Found scheduler attached to this flow.", "yellow")
            cprint("Sending SIGKILL to the scheduler before cancelling the tasks!", "yellow")

            with open(self.pid_file, "rt") as fh:
                pid = int(fh.readline())

            retcode = os.system("kill -9 %d" % pid)
            self.history.info("Sent SIGKILL to the scheduler, retcode: %s" % retcode)
            try:
                os.remove(self.pid_file)
            except IOError:
                pass

        num_cancelled = 0
        for task in self.iflat_tasks(nids=nids):
            num_cancelled += task.cancel()

        return num_cancelled