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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
.. _migration:
Migration v4
============
ReactiveX for Python v4 is an evolution of RxPY v3 to modernize it to
current Python standards:
- Project main module renamed from ``rx`` to ``reactivex``. This is done
to give it a unique name different from the obsolete `Reactive Extensions
(RxPY) <https://github.com/Reactive-Extensions/RxPy>`_
- Generic type annotations. Code now type checks with pyright / pylance
at strict settings. It also mostly type checks with mypy. Mypy
should eventually catch up.
- The ``pipe`` function has been renamed to ``compose``. There is now a
new function ``pipe`` that works similar to the ``pipe`` method.
- RxPY is now a modern Python project using ``pyproject.toml`` instead
of ``setup.py``, and using modern tools such as Poetry, Black
formatter and isort.
.. code:: python
import reactivex as rx
from reactivex import operators as ops
rx.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon").pipe(
ops.map(lambda s: len(s)),
ops.filter(lambda i: i >= 5)
).subscribe(lambda value: print("Received {0}".format(value)))
Migration v3
============
RxPY v3 is a major evolution from RxPY v1. This release brings many
improvements, some of the most important ones being:
* A better integration in IDEs via autocompletion support.
* New operators can be implemented outside of RxPY.
* Operator chains are now built via the :func:`pipe <rx.Observable.pipe>` operator.
* A default scheduler can be provided in an operator chain.
Pipe Based Operator Chaining
-----------------------------
The most fundamental change is the way operators are chained together. On RxPY
v1, operators were methods of the Observable class. So they were chained by
using the existing Observable methods:
.. code:: python
from rx import Observable
Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \
.map(lambda s: len(s)) \
.filter(lambda i: i >= 5) \
.subscribe(lambda value: print("Received {0}".format(value)))
Chaining in RxPY v3 is based on the :func:`pipe <rx.Observable.pipe>` operator.
This operator is now one of the only methods of the
:class:`Observable <rx.Observable>` class. In RxPY v3, operators are implemented
as functions:
.. code:: python
import rx
from rx import operators as ops
rx.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon").pipe(
ops.map(lambda s: len(s)),
ops.filter(lambda i: i >= 5)
).subscribe(lambda value: print("Received {0}".format(value)))
The fact that operators are functions means that adding new operators is now
very easy. Instead of wrapping custom operators with the *let* operator, they can
be directly used in a pipe chain.
Removal Of The Result Mapper
-----------------------------
The mapper function is removed in operators that combine the values of several
observables. This change applies to the following operators:
:func:`combine_latest <rx.operators.combine_latest>`,
:func:`group_join <rx.operators.group_join>`,
:func:`join <rx.operators.join>`,
:func:`with_latest_from <rx.operators.with_latest_from>`,
:func:`zip <rx.operators.zip>`, and
:func:`zip_with_iterable <rx.operators.zip_with_iterable>`.
In RxPY v1, these operators were used the following way:
.. code:: python
from rx import Observable
import operator
a = Observable.of(1, 2, 3, 4)
b = Observable.of(2, 2, 4, 4)
a.zip(b, lambda a, b: operator.mul(a, b)) \
.subscribe(print)
Now they return an Observable of tuples, with each item being the combination of
the source Observables:
.. code:: python
import rx
from rx import operators as ops
import operator
a = rx.of(1, 2, 3, 4)
b = rx.of(2, 2, 4, 4)
a.pipe(
ops.zip(b), # returns a tuple with the items of a and b
ops.map(lambda z: operator.mul(z[0], z[1]))
).subscribe(print)
Dealing with the tuple unpacking is made easier with the starmap operator that
unpacks the tuple to args:
.. code:: python
import rx
from rx import operators as ops
import operator
a = rx.of(1, 2, 3, 4)
b = rx.of(2, 2, 4, 4)
a.pipe(
ops.zip(b),
ops.starmap(operator.mul)
).subscribe(print)
Scheduler Parameter In Create Operator
---------------------------------------
The subscription function provided to the :func:`create <rx.create>` operator
now takes two parameters: An observer and a scheduler. The scheduler parameter
is new: If a scheduler has been set in the call to subscribe, then this
scheduler is passed to the subscription function. Otherwise this parameter is
set to *None*.
One can use or ignore this parameter. This new scheduler parameter allows the
create operator to use the default scheduler provided in the subscribe call. So
scheduling item emissions with relative or absolute due-time is now possible.
Removal Of List Of Observables
-------------------------------
The support of list of Observables as a parameter has been removed in the
following operators:
:func:`merge <rx.merge>`,
:func:`zip <rx.zip>`, and
:func:`combine_latest <rx.combine_latest>`.
For example in RxPY v1 the *merge* operator could be called with a list:
.. code:: python
from rx import Observable
obs1 = Observable.from_([1, 2, 3, 4])
obs2 = Observable.from_([5, 6, 7, 8])
res = Observable.merge([obs1, obs2])
res.subscribe(print)
This is not possible anymore in RxPY v3. So Observables must be provided
explicitly:
.. code:: python
import rx, operator as op
obs1 = rx.from_([1, 2, 3, 4])
obs2 = rx.from_([5, 6, 7, 8])
res = rx.merge(obs1, obs2)
res.subscribe(print)
If for any reason the Observables are only available as a list, then they can be
unpacked:
.. code:: python
import rx
from rx import operators as ops
obs1 = rx.from_([1, 2, 3, 4])
obs2 = rx.from_([5, 6, 7, 8])
obs_list = [obs1, obs2]
res = rx.merge(*obs_list)
res.subscribe(print)
Blocking Observable
-------------------
BlockingObservables have been removed from rxPY v3. In RxPY v1, blocking until
an Observable completes was done the following way:
.. code:: python
from rx import Observable
res = Observable.from_([1, 2, 3, 4]).to_blocking().last()
print(res)
This is now done with the :func:`run <rx.Observable.run>` operator:
.. code:: python
import rx
res = rx.from_([1, 2, 3, 4]).run()
print(res)
The *run* operator returns only the last value emitted by the source
Observable. It is possible to use the previous blocking operators by using the
standard operators before *run*. For example:
* Get first item: obs.pipe(ops.first()).run()
* Get all items: obs.pipe(ops.to_list()).run()
Back-Pressure
--------------
Support for back-pressure - and so ControllableObservable - has been removed in
RxPY v3. Back-pressure can be implemented in several ways, and many strategies
can be adopted. So we consider that such features are beyond the scope of RxPY.
You are encouraged to provide independent implementations as separate packages
so that they can be shared by the community.
List of community projects supporting backpressure can be found in
:ref:`additional_reading`.
Time Is In Seconds
------------------
Operators that take time values as parameters now use seconds as a unit instead
of milliseconds. This RxPY v1 example:
.. code:: python
ops.debounce(500)
is now written as:
.. code:: python
ops.debounce(0.5)
Packages Renamed
----------------
Some packages were renamed:
+-----------------------+-------------------------+
| Old name | New name |
+-----------------------+-------------------------+
| *rx.concurrency* | *reactivex.scheduler* |
+-----------------------+-------------------------+
| *rx.disposables* | *rx.disposable* |
+-----------------------+-------------------------+
| *rx.subjects* | *rx.subject* |
+-----------------------+-------------------------+
Furthermore, the package formerly known as *rx.concurrency.mainloopscheduler*
has been split into two parts, *reactivex.scheduler.mainloop* and
*reactivex.scheduler.eventloop*.
|