forked from techstay/python-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py
More file actions
26 lines (19 loc) · 653 Bytes
/
scheduler.py
File metadata and controls
26 lines (19 loc) · 653 Bytes
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
import rx
from rx.scheduler import ThreadPoolScheduler
from rx import operators as op
import multiprocessing
import time
import threading
import random
def long_work(value):
time.sleep(random.randint(5, 20) / 10)
return value
pool_schedular = ThreadPoolScheduler(multiprocessing.cpu_count())
rx.range(5).pipe(
op.map(lambda i: long_work(i + 1)),
op.subscribe_on(pool_schedular)
).subscribe(lambda i: print(f'Work 1: {threading.current_thread().name}, {i}'))
rx.of(1, 2, 3, 4, 5).pipe(
op.map(lambda i: i * 2),
op.subscribe_on(pool_schedular)
).subscribe(lambda i: print(f'Work 2: {threading.current_thread().name}, {i}'))