RxPY 实用操作符
delay
这个操作符将根据给定的时间或日期延迟源可观察对象的发射。
语法
delay(timespan)
参数
timespan:这将是以秒为单位或日期的时间。
返回值
它将返回一个可观察的对象,其中包含超时后发出的源值。
示例
from rx import of, operators as op
import datetime
test1 = of(1,2,3,4,5)
sub1 = test1.pipe(
op.delay(5.0)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
input("Press any key to exit\n")
输出
E:\pyrx>python testrx.py
Press any key to exit
The value is 1
The value is 2
The value is 3
The value is 4
The value is 5
materialize
这个操作符将会将源Observable中的值转换成显式通知值的形式进行发出。
语法
materialize()
返回值
这将返回一个可观测对象,以明确的通知值形式发出的值。
示例
from rx import of, operators as op
import datetime
test1 = of(1,2,3,4,5)
sub1 = test1.pipe(
op.materialize()
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py
The value is OnNext(1.0)
The value is OnNext(2.0)
The value is OnNext(3.0)
The value is OnNext(4.0)
The value is OnNext(5.0)
The value is OnCompleted()
time_interval
此运算符将给出源可观察对象中的值之间经过的时间。
语法
time_interval()
返回值
它将返回一个可观察对象,其中包含源值发出之间经过的时间。
示例
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
sub1 = test.pipe(
op.time_interval()
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py
The value is TimeInterval(value=1, interval=datetime.timedelta(microseconds=1000
))
The value is TimeInterval(value=2, interval=datetime.timedelta(0))
The value is TimeInterval(value=3, interval=datetime.timedelta(0))
The value is TimeInterval(value=4, interval=datetime.timedelta(microseconds=1000
))
The value is TimeInterval(value=5, interval=datetime.timedelta(0))
The value is TimeInterval(value=6, interval=datetime.timedelta(0))
timeout
这个操作符会在经过指定时间后从源可观察对象中发出所有的值,否则会触发一个错误。
语法
timeout(duetime)
参数
duetime: 给定的时间,以秒为单位。
返回值
它将返回一个可观察对象,其中包含来自源可观察对象的所有值。
示例
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
sub1 = test.pipe(
op.timeout(5.0)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py
The value is 1
The value is 2
The value is 3
The value is 4
The value is 5
The value is 6
timestamp
此操作符会将来自源可观察对象的所有值都附加上时间戳。
语法
timestamp()
返回值
它将返回一个包含源可观察对象中所有值及其时间戳的可观察对象。
示例
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
sub1 = test.pipe(
op.timestamp()
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py
The value is Timestamp(value=1, timestamp=datetime.datetime(2019, 11, 4, 4, 57,
44, 667243))
The value is Timestamp(value=2, timestamp=datetime.datetime(2019, 11, 4, 4, 57,
44, 668243))
The value is Timestamp(value=3, timestamp=datetime.datetime(2019, 11, 4, 4, 57,
44, 668243))
The value is Timestamp(value=4, timestamp=datetime.datetime(2019, 11, 4, 4, 57,
44, 668243))
The value is Timestamp(value=5, timestamp=datetime.datetime(2019, 11, 4, 4, 57,
44, 669243))
The value is Timestamp(value=6, timestamp=datetime.datetime(2019, 11, 4, 4, 57,
44, 669243))