<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_event(self, event):
 """Writes an event proto to disk. This method is threadsafe with respect to invocations of itself. Args: event: The event proto. Raises: IOError: If writing the event proto to disk fails. """

    self._lock.acquire()
    try:
      self._events_writer.WriteEvent(event)
      self._event_count += 1
      if self._always_flush:
        # We flush on every event within the integration test.
        self._events_writer.Flush()

      if self._event_count == self._check_this_often:
        # Every so often, we check whether the size of the file is too big.
        self._event_count = 0

        # Flush to get an accurate size check.
        self._events_writer.Flush()

        file_path = os.path.join(self._events_directory,
                                 self.get_current_file_name())
        if not tf.io.gfile.exists(file_path):
          # The events file does not exist. Perhaps the user had manually
          # deleted it after training began. Create a new one.
          self._events_writer.Close()
          self._events_writer = self._create_events_writer(
              self._events_directory)
        elif tf.io.gfile.stat(file_path).length > self._single_file_size_cap_bytes:
          # The current events file has gotten too big. Close the previous
          # events writer. Make a new one.
          self._events_writer.Close()
          self._events_writer = self._create_events_writer(
              self._events_directory)
    except IOError as err:
      logger.error(
          "Writing to %s failed: %s", self.get_current_file_name(), err)
    self._lock.release()