python-3.xpython-multiprocessingstarmap

Python starmap got multiple values for argument


I hava a multiprocess program whcich using starmap. But when I run it, a TypeError occured. While normally when I run it in map.

def process_single_image(img_path, target_dir="", func=None, severity=1):
    print(target_dir)
    ...
pool.starmap(
    partial(
        process_single_image,
        target_dir=target_dir,
        func=iaa.imgcorruptlike.apply_shot_noise,
        severity=4,
    ),
    img_paths,
)

I don't know why I got this error. I think it should run normally as pool.map(....). Here is the traceback. enter image description here


Solution

  • The arguments you pass to .starmap are unpacked to the target function. Therefore, the arguments must be an iterable of iterables (even if the argument length the target function accepts is one).

    Therefore, if the argument length is one, then explicitly convert each element of img_path into a tuple before passing it to starmap:

    img_path = [(arg,) for arg in img_path]