Skip to main content

examples/sleepy_multiply.py

1#!/usr/bin/env python
2"""
3Multiply two numbers together, but with a delay before returning.
4This mimics a computation that has to be fetched from a remote server.
6This example also shows how to handle functions that take multiple inputs.
7"""
9import time
11from concurrently import concurrently
14def sleepy_multiply(x, y):
15 """
16 Multiply two numbers together, but with a random delay.
17 """
18 time.sleep(x / 10)
19 return x * y
22if __name__ == "__main__":
23 inputs = [
24 (1, 2),
25 (2, 3),
26 (7, 4),
27 (3, 1),
28 (5, 2),
29 (4, 9),
30 (7, 2),
31 (6, 1),
32 ]
34 for (x, y), output in concurrently(lambda x: sleepy_multiply(*x), inputs=inputs):
35 print(x, "*", y, "=", output)