[Python]#10 variations of Map(feat. Multiprocessing)

Clay Ryu's sound lab·2023년 11월 30일
0

Framework

목록 보기
35/48

posted with the help of GPT-4

  1. map: This built-in Python function applies a function to every item in an iterable and returns a list of results. All items are processed in order, and the function waits for all computations to finish before returning the results.
def square(n):
    return n * n

result = map(square, [1, 2, 3, 4, 5])
print(list(result))  # prints: [1, 4, 9, 16, 25]
  1. imap: This function from Python's multiprocessing module is similar to map, but it returns an iterator that yields results as soon as they are ready. This is useful for long-running tasks, as it allows you to start processing results before all computations are finished.
from multiprocessing import Pool

def square(n):
    return n * n

with Pool() as p:
    result = p.imap(square, [1, 2, 3, 4, 5])
    for num in result:
        print(num)  # prints: 1, 4, 9, 16, 25 (one at a time, as soon as they are ready)
  1. imap_unordered: This function is similar to imap, but it doesn't guarantee that results will be yielded in the same order that they were put in. This can be more efficient when dealing with tasks of varying lengths, as it allows results from quick tasks to be returned before slow ones.

  2. starmap: This function is similar to imap, but it unpacks each item in the iterable as arguments to the function. This is useful when your function requires multiple arguments.

from multiprocessing import Pool

def multiply(a, b):
    return a * b

with Pool() as p:
    result = p.starmap(multiply, [(1, 2), (3, 4), (5, 6)])
    print(list(result))  # prints: [2, 12, 30]
  1. map_async: This function is similar to map, but it returns immediately without waiting for the results to be ready. Instead, it returns a AsyncResult object, which can be used to retrieve the results when they are ready. This is useful when you want to do other computations while waiting for the map operation to finish.
from multiprocessing import Pool

def square(n):
    return n * n

with Pool() as p:
    result = p.map_async(square, [1, 2, 3, 4, 5])
    print(result.get())  # waits for results and prints: [1, 4, 9, 16, 25]
profile
chords & code // harmony with structure

0개의 댓글