<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _close(self, status, do_callbacks=True):
 """ Takes the status on which it should leave the connection and an optional boolean parameter to dispatch the disconnected and close callbacks if there are any. """

        if self.is_closed:
            self._status = status
            return
        self._status = Client.CLOSED

        # Stop background tasks
        yield self._end_flusher_loop()

        if self._ping_timer is not None and self._ping_timer.is_running():
            self._ping_timer.stop()

        if not self.io.closed():
            self.io.close()

        # Cleanup subscriptions since not reconnecting so no need
        # to replay the subscriptions anymore.
        for ssid, sub in self._subs.items():
            self._subs.pop(ssid, None)
            self._remove_subscription(sub)

        if do_callbacks:
            if self._disconnected_cb is not None:
                self._disconnected_cb()
            if self._closed_cb is not None:
                self._closed_cb()