Exemplo n.º 1
0
def open_file(file_name, mode):
    # type: (str, str) -> str
    """ Opens a file (retrieves if necessary).

    Calls the external python library (that calls the bindings-common)
    in order to request a file.

    :param file_name: <String> File name.
    :param mode: Open file mode ('r', 'rw', etc.).
    :return: The current name of the file requested (that may have been
             renamed during runtime).
    """
    app_id = 0
    compss_mode = get_compss_direction(mode)
    if __debug__:
        logger.debug("Getting file %s with mode %s" % (file_name, compss_mode))
    compss_name = COMPSs.open_file(app_id, file_name, compss_mode)
    if __debug__:
        logger.debug("COMPSs file name is %s" % compss_name)
    return compss_name
Exemplo n.º 2
0
def _synchronize(obj, mode):
    # type: (object, int) -> object
    """ Synchronization function.

    This method retrieves the value of a future object.
    Calls the runtime in order to wait for the value and returns it when
    received.

    :param obj: Object to synchronize.
    :param mode: Direction of the object to synchronize.
    :return: The value of the object requested.
    """
    # TODO: Add a boolean to differentiate between files and object on the
    # COMPSs.open_file call. This change pretends to obtain better traces.
    # Must be implemented first in the Runtime, then in the bindings common
    # C API and finally add the boolean here
    app_id = 0
    if is_psco(obj):
        obj_id = get_id(obj)
        if not OT_is_pending_to_synchronize(obj_id):
            return obj
        else:
            # file_path is of the form storage://pscoId or
            # file://sys_path_to_file
            file_path = COMPSs.open_file(app_id,
                                         "".join(("storage://", str(obj_id))),
                                         mode)
            # TODO: Add switch on protocol (first parameter returned currently ignored)
            _, file_name = file_path.split("://")
            new_obj = get_by_id(file_name)
            OT_stop_tracking(obj)
            return new_obj

    obj_id = OT_is_tracked(obj)
    if obj_id is None:  # Not being tracked
        return obj
    if not OT_is_pending_to_synchronize(obj_id):
        return obj

    if __debug__:
        logger.debug("Synchronizing object %s with mode %s" % (obj_id, mode))

    file_name = OT_get_file_name(obj_id)
    compss_file = COMPSs.open_file(app_id, file_name, mode)

    # Runtime can return a path or a PSCOId
    if compss_file.startswith('/'):
        # If the real filename is null, then return None. The task that
        # produces the output file may have been ignored or cancelled, so its
        # result does not exist.
        real_file_name = compss_file.split('/')[-1]
        if real_file_name == "null":
            print("WARNING: Could not retrieve the object " + str(file_name) +
                  " since the task that produces it may have been IGNORED or CANCELLED. Please, check the logs. Returning None.")  # noqa: E501
            return None
        new_obj = deserialize_from_file(compss_file)
        COMPSs.close_file(app_id, file_name, mode)
    else:
        new_obj = get_by_id(compss_file)

    if mode == 'r':
        OT_update_mapping(obj_id, new_obj)

    if mode != 'r':
        COMPSs.delete_file(app_id, OT_get_file_name(obj_id), False)
        OT_stop_tracking(obj)

    return new_obj