Skip to content

Add istarmap function. Combination between imap and starmap. #107390

Closed as not planned
@liortct

Description

@liortct

Feature or enhancement

In python multiprocessing module we have three built in functions:

  1. map - apply function to each element in iterable and return a list of the result.
  2. starmap - like map method but the element of the iterable are expected to be unpacked as arguments. It's used for function with multiple parameters)
  3. imap- like map method but return an iterable result and not list. It's used when we want to see the result of the functions before all the function are finish.

You can see reference of multiprocessing pool here.

I want to add a new method to multiprocessing module called istarmap.
istarmap will be combination of starmap and imap.
istarmap will get iterable that are expected to be unpacked as arguments like starmap.
istarmap will return and iterable of the result like imap.

Pitch

istarmap will be useful in same cases that imap is useful but will support unpacked arguments for function with multiple arguments like starmap.

You can see a question in StackOverflow exactly on this case here.

Example

import multiprocessing as mp
import time

def func(x, y):
    time.sleep(10)
    return f"{x=}, {y=}"

with mp.Pool() as pool:
    arguments = [(1, 2), (5,3), (8,4)]
    results = pool.istarmap(func, arguments)
    for r in results:
        print(r)

We can see here example of use of istarmap.
We have a function with multiple parameters and we want to work with multiprocess and print each result that we have to the screen.

In addiction, I would like to say that I will be happy to add this feature as my first contribute to cpython project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions