ReactiveX for Python (RxPY)
repository·master·Indexed 26 days ago
https://github.com/reactivex/rxpyA library for composing asynchronous and event-based programs using observable sequences and pipable query operators in Python. RxPY supports both functional (pipe-based) and fluent (method chaining) styles for transforming emissions. It provides tools for CPU concurrency via Schedulers, IO concurrency with AsyncIO, and custom operator implementation. Version 5.1.0 supports Python 3.10 through 3.14.
What's inside RxPY
- ReactiveX for Python (RxPY) is a library designed for composing asynchronous and event-based programs. It achieves this by using observable collections and pipable query operators within Python.
Understand the core concepts of RxPY
masterReactiveX for Python (RxPY) is used to compose asynchronous and event-based programs. It relies on three primary concepts:
- Observables: Represent asynchronous data streams (e.g., stock quotes, Tweets, computer events, or web service requests).
- Observers: Objects that subscribe to an Observable and are notified whenever an event occurs in the stream.
- Operators: Functions used to query and transform data streams. These can be chained using the
pipeoperator to perform operations likefilter,map,reduce, and time-based transformations. - Schedulers: Used to parameterize concurrency within data and event streams.
Explore official ReactiveX documentation and tutorials
masterFor foundational knowledge, operator details, and guided tutorials, refer to the official ReactiveX resources:
- Introduction: General overview of ReactiveX.
- Tutorials: Step-by-step learning paths.
- Operators: Detailed documentation of available operators.
http://reactivex.io/intro.html http://reactivex.io/tutorials.html http://reactivex.io/documentation/operators.htmlMigrate from RxPY v3 to v4: Renamed Module and Types
masterWhen migrating from v3 to v4:
- The main module was renamed from
rxtoreactivex. - Generic type annotations were added for better compatibility with
pyrightandmypy. - The
pipefunction was renamed tocompose, and a newpipemethod was introduced to work similarly to thepipemethod on Observables.
- The main module was renamed from
Migrate from RxPY v3 to v4: Handling Combiner Operators
masterIn RxPY v3, operators that combine multiple Observables (like
zip,combine_latest,join, etc.) used a result mapper function. In v4+, these operators no longer accept a mapper; instead, they return anObservableof tuples. To process these tuples, useops.maporops.starmap.import reactivex as rx from reactivex 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.starmap(operator.mul) ).subscribe(print)Migrate from RxPY v4.x to v5.x
masterRxPY v4.x only supported the functional (pipe-based) style. Because RxPY v5.x maintains full backward compatibility, all existing v4.x code using
.pipe()andreactivex.operatorswill continue to work without any changes. There are no breaking changes for functional-style code.# RxPY v4.x code works unchanged in v5.x from reactivex import operators as ops result = source.pipe( ops.map(lambda x: x * 2), ops.filter(lambda x: x > 5) )Set a Default Scheduler for an Observable Chain
masterInstead of providing a scheduler to every individual operator, you can specify a default scheduler for the entire chain by passing it to the
.subscribe()method.Operators will select a scheduler in this priority order:
- The scheduler explicitly provided to the operator.
- The default scheduler provided in the
subscribecall. - The operator's own internal default scheduler.
Python Version Support for RxPY v5
masterRxPY v5 supports Python 3.10 through 3.14. Support for Python 3.8 and 3.9 has been dropped.Transform Observables using Functional and Fluent syntax
masterRxPY supports two styles for chaining operators to transform emissions:
- Functional style: Uses the
.pipe()method to pass operators (imported fromreactivex.operators). This is often preferred for readability in complex pipelines. - Fluent style: Uses method chaining directly on the Observable object (e.g.,
.map().filter()).
Note: In v4,
rx.pipewas renamed tocomposefor internal composition, but the.pipe()method on Observables remains the standard for building pipelines.# Functional style from reactivex import of, operators as op source = of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") composed = source.pipe( op.map(lambda s: len(s)), op.filter(lambda i: i >= 5) ) # Fluent style composed = source.map(lambda s: len(s)).filter(lambda i: i >= 5)- Functional style: Uses the
Migrate from RxPY v3 to v4: Passing Observables to Merge/Zip/CombineLatest
masterIn RxPY v3,
merge,zip, andcombine_latestaccepted a list of Observables. In v4+, you must provide Observables as individual arguments. If you have a list, unpack it using the*operator.import reactivex as rx from reactivex import operators as ops obs1 = rx.from_([1, 2, 3, 4]) obs2 = rx.from_([5, 6, 7, 8]) obs_list = [obs1, obs2] # Unpack the list to pass as individual arguments res = rx.merge(*obs_list)Install ReactiveX for Python (RxPY) v5
masterTo use RxPY v5, ensure you are running Python 3.10 or above. You can install the package using
pip3.pip3 install reactivexMigrate from RxPY v3 to v4: Replace BlockingObservables with .run()
masterThe
BlockingObservablesAPI was removed in v3. To block until an Observable completes and get a result, use the.run()operator.- To get the last value:
obs.run() - To get the first item:
obs.pipe(ops.first()).run() - To get all items as a list:
obs.pipe(ops.to_list()).run()
import reactivex as rx res = rx.from_([1, 2, 3, 4]).run() print(res)- To get the last value: