Client Mapping TasksΒΆ
Use map() to submit the same function across many inputs in parallel.
In the example below, a local scheduler and workers are started with
SchedulerClusterCombo. The client then connects to the scheduler address and calls
map().
"""Basic Client.map and Client.starmap example with a local cluster."""
import math
from scaler import Client
from scaler.cluster.combo import SchedulerClusterCombo
def add(x, y):
return x + y
def main():
cluster = SchedulerClusterCombo(n_workers=10)
with Client(address=cluster.get_address()) as client:
results = client.map(math.sqrt, range(100))
print(sum(results))
results = client.map(add, [1, 2, 3], [10, 20, 30])
print(results)
results = client.starmap(add, [(1, 10), (2, 20), (3, 30)])
print(results)
cluster.shutdown()
if __name__ == "__main__":
main()
What the example does:
Starts a local scheduler + workers with
SchedulerClusterCombo.Uses
map()to runmath.sqrtacrossrange(100).Uses
map()with two iterables for pairwise arguments.Uses
starmap()when arguments are already grouped as tuples.
When to choose each API:
Use
map()when arguments come from one or more parallel iterables.Use
starmap()when each task input is a prebuilt argument tuple.