File: timer.py

package info (click to toggle)
python-rx 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,204 kB
  • sloc: python: 39,525; javascript: 77; makefile: 24
file content (28 lines) | stat: -rw-r--r-- 496 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import concurrent.futures
import time

import reactivex
from reactivex import operators as ops

seconds = [5, 1, 2, 4, 3]


def sleep(tm: float) -> float:
    time.sleep(tm)
    return tm


def output(result: str) -> None:
    print("%d seconds" % result)


with concurrent.futures.ProcessPoolExecutor(5) as executor:
    reactivex.from_(seconds).pipe(
        ops.flat_map(lambda s: executor.submit(sleep, s))
    ).subscribe(output)

# 1 seconds
# 2 seconds
# 3 seconds
# 4 seconds
# 5 seconds