Beispiel #1
0
def create_video_subproc(
    frames: List[np.ndarray],
    path: str,
    filename: str,
    extension: str = ".gif",
    fps: int = 15,
    daemon: bool = True,
):
    """
    Create video with a subprocess, since it takes a lot of time for ``moviepy``
    to encode the video file.

    See Also:
         :func:`.create_video`

    Note:
        if ``daemon`` is true, then this function cannot be used in a
        daemonic subprocess.

    Args:
        frames: A list of numpy arrays of shape (H, W, C) or (H, W), and with
            ``dtype`` = any float or any int.
            When a frame is float type, its value range should be [0, 1].
            When a frame is integer type, its value range should be [0, 255].
        path: Directory to save the video.
        filename: File name.
        extension: File extension.
        fps: frames per second.
        daemon: Whether launching the saving process as a daemonic process.

    Returns:
        A wait function, once called, block until creation has finished.
    """

    def wait():
        pass

    if frames:
        p = get_context("spawn").Process(
            target=create_video, args=(frames, path, filename, extension, fps)
        )
        p.daemon = daemon
        p.start()

        def wait():
            p.join()

    return wait
Beispiel #2
0
def create_image_subproc(
    image: np.array,
    path: str,
    filename: str,
    extension: str = ".png",
    daemon: bool = True,
):
    """
    Create image with a subprocess.

    See Also:
         :func:`.create_image`

    Note:
        if ``daemon`` is true, then this function cannot be used in a
        daemonic subprocess.

    Args:
        image: A numpy array of shape (H, W, C) or (H, W), and with
            ``dtype`` = any float or any int.
            When a frame is float type, its value range should be [0, 1].
            When a frame is integer type, its value range should be [0, 255].
        path: Directory to save the image.
        filename: File name.
        extension: File extension.
        daemon: Whether launching the saving process as a daemonic process.

    Returns:
        A wait function, once called, block until creation has finished.
    """
    p = get_context("spawn").Process(
        target=create_image, args=(image, path, filename, extension)
    )
    p.daemon = daemon
    p.start()

    def wait():
        p.join()

    return wait
Beispiel #3
0
from machin.parallel.pickle import dumps, loads
from machin.parallel.process import Process
from machin.parallel import get_context
import torch as t


def print_tensor_sub_proc(tens):
    print(loads(tens))


def exec_sub_proc(func):
    loads(func)()


if __name__ == "__main__":
    spawn_ctx = get_context("spawn")
    fork_ctx = get_context("fork")
    # cpu tensor, not in shared memory
    # If you would like to pass this tensor to a sub process
    # set copy_tensor to `True`, otherwise only a pointer to
    # memory will be passed to the subprocess.
    # However, if you do this in the same process, no SEGFAULT
    # will happen, because memory map is the same.
    tensor = t.ones([10])
    p = Process(target=print_tensor_sub_proc,
                args=(dumps(tensor, copy_tensor=True), ),
                ctx=fork_ctx)
    p.start()
    p.join()
    # cpu tensor, in shared memory