Skip to main content

Python Stream Processing. A Faust fork

Project description

faust

Python Stream Processing Fork

python versions version codecov slack Code style: black pre-commit license downloads

Installation

pip install faust-streaming

Documentation

Why the fork

We have decided to fork the original Faust project because there is a critical process of releasing new versions which causes uncertainty in the community. Everybody is welcome to contribute to this fork, and you can be added as a maintainer.

We want to:

  • Ensure continues release
  • Code quality
  • Use of latest versions of kafka drivers (for now only aiokafka)
  • Support kafka transactions
  • Update the documentation

and more...

Usage

# Python Streams
# Forever scalable event processing & in-memory durable K/V store;
# as a library w/ asyncio & static typing.
import faust

Faust is a stream processing library, porting the ideas from Kafka Streams to Python.

It is used at Robinhood to build high performance distributed systems and real-time data pipelines that process billions of events every day.

Faust provides both stream processing and event processing, sharing similarity with tools such as Kafka Streams, Apache Spark, Storm, Samza, Flink,

It does not use a DSL, it's just Python! This means you can use all your favorite Python libraries when stream processing: NumPy, PyTorch, Pandas, NLTK, Django, Flask, SQLAlchemy, ++

Faust requires Python 3.6 or later for the new async/await_ syntax, and variable type annotations.

Here's an example processing a stream of incoming orders:

app = faust.App('myapp', broker='kafka://localhost')

# Models describe how messages are serialized:
# {"account_id": "3fae-...", amount": 3}
class Order(faust.Record):
    account_id: str
    amount: int

@app.agent(value_type=Order)
async def order(orders):
    async for order in orders:
        # process infinite stream of orders.
        print(f'Order for {order.account_id}: {order.amount}')

The Agent decorator defines a "stream processor" that essentially consumes from a Kafka topic and does something for every event it receives.

The agent is an async def function, so can also perform other operations asynchronously, such as web requests.

This system can persist state, acting like a database. Tables are named distributed key/value stores you can use as regular Python dictionaries.

Tables are stored locally on each machine using a super fast embedded database written in C++, called RocksDB.

Tables can also store aggregate counts that are optionally "windowed" so you can keep track of "number of clicks from the last day," or "number of clicks in the last hour." for example. Like Kafka Streams, we support tumbling, hopping and sliding windows of time, and old windows can be expired to stop data from filling up.

For reliability, we use a Kafka topic as "write-ahead-log". Whenever a key is changed we publish to the changelog. Standby nodes consume from this changelog to keep an exact replica of the data and enables instant recovery should any of the nodes fail.

To the user a table is just a dictionary, but data is persisted between restarts and replicated across nodes so on failover other nodes can take over automatically.

You can count page views by URL:

# data sent to 'clicks' topic sharded by URL key.
# e.g. key="http://example.com" value="1"
click_topic = app.topic('clicks', key_type=str, value_type=int)

# default value for missing URL will be 0 with `default=int`
counts = app.Table('click_counts', default=int)

@app.agent(click_topic)
async def count_click(clicks):
    async for url, count in clicks.items():
        counts[url] += count

The data sent to the Kafka topic is partitioned, which means the clicks will be sharded by URL in such a way that every count for the same URL will be delivered to the same Faust worker instance.

Faust supports any type of stream data: bytes, Unicode and serialized structures, but also comes with "Models" that use modern Python syntax to describe how keys and values in streams are serialized:

# Order is a json serialized dictionary,
# having these fields:

class Order(faust.Record):
    account_id: str
    product_id: str
    price: float
    quantity: float = 1.0

orders_topic = app.topic('orders', key_type=str, value_type=Order)

@app.agent(orders_topic)
async def process_order(orders):
    async for order in orders:
        # process each order using regular Python
        total_price = order.price * order.quantity
        await send_order_received_email(order.account_id, order)

Faust is statically typed, using the mypy type checker, so you can take advantage of static types when writing applications.

The Faust source code is small, well organized, and serves as a good resource for learning the implementation of Kafka Streams.

Learn more about Faust in the introduction introduction page to read more about Faust, system requirements, installation instructions, community resources, and more.

or go directly to the quickstart tutorial to see Faust in action by programming a streaming application.

then explore the User Guide for in-depth information organized by topic.

Local development

  1. Clone the project
  2. Create a virtualenv: python3.7 -m venv venv && source venv/bin/activate
  3. Install the requirements: ./scripts/install
  4. Run lint: ./scripts/lint
  5. Run tests: ./scripts/tests

Faust key points

Simple

Faust is extremely easy to use. To get started using other stream processing solutions you have complicated hello-world projects, and infrastructure requirements. Faust only requires Kafka, the rest is just Python, so If you know Python you can already use Faust to do stream processing, and it can integrate with just about anything.

Here's one of the easier applications you can make::

import faust

class Greeting(faust.Record):
    from_name: str
    to_name: str

app = faust.App('hello-app', broker='kafka://localhost')
topic = app.topic('hello-topic', value_type=Greeting)

@app.agent(topic)
async def hello(greetings):
    async for greeting in greetings:
        print(f'Hello from {greeting.from_name} to {greeting.to_name}')

@app.timer(interval=1.0)
async def example_sender(app):
    await hello.send(
        value=Greeting(from_name='Faust', to_name='you'),
    )

if __name__ == '__main__':
    app.main()

You're probably a bit intimidated by the async and await keywords, but you don't have to know how asyncio works to use Faust: just mimic the examples, and you'll be fine.

The example application starts two tasks: one is processing a stream, the other is a background thread sending events to that stream. In a real-life application, your system will publish events to Kafka topics that your processors can consume from, and the background thread is only needed to feed data into our example.

Highly Available

Faust is highly available and can survive network problems and server crashes. In the case of node failure, it can automatically recover, and tables have standby nodes that will take over.

Distributed

Start more instances of your application as needed.

Fast

A single-core Faust worker instance can already process tens of thousands of events every second, and we are reasonably confident that throughput will increase once we can support a more optimized Kafka client.

Flexible

Faust is just Python, and a stream is an infinite asynchronous iterator. If you know how to use Python, you already know how to use Faust, and it works with your favorite Python libraries like Django, Flask, SQLAlchemy, NLTK, NumPy, SciPy, TensorFlow, etc.

Bundles

Faust also defines a group of setuptools extensions that can be used to install Faust and the dependencies for a given feature.

You can specify these in your requirements or on the pip command-line by using brackets. Separate multiple bundles using the comma:

pip install "faust-streaming[rocksdb]"

pip install "faust-streaming[rocksdb,uvloop,fast,redis,aerospike]"

The following bundles are available:

Faust with extras

Stores

RocksDB

For using RocksDB for storing Faust table state. Recommended in production.

pip install faust-streaming[rocksdb] (uses RocksDB 6)

pip install faust-streaming[rocksdict] (uses RocksDB 8, not backwards compatible with 6)

Aerospike

pip install faust-streaming[aerospike] for using Aerospike for storing Faust table state. Recommended if supported

Aerospike Configuration

Aerospike can be enabled as the state store by specifying store="aerospike://"

By default, all tables backed by Aerospike use use_partitioner=True and generate changelog topic events similar to a state store backed by RocksDB. The following configuration options should be passed in as keys to the options parameter in Table namespace : aerospike namespace

ttl: TTL for all KV's in the table

username: username to connect to the Aerospike cluster

password: password to connect to the Aerospike cluster

hosts : the hosts parameter as specified in the aerospike client

policies: the different policies for read/write/scans policies

client: a dict of host and policies defined above

Caching

faust-streaming[redis] for using Redis as a simple caching backend (Memcached-style).

Codecs

faust-streaming[yaml] for using YAML and the PyYAML library in streams.

Optimization

faust-streaming[fast] for installing all the available C speedup extensions to Faust core.

Sensors

faust-streaming[datadog] for using the Datadog Faust monitor.

faust-streaming[statsd] for using the Statsd Faust monitor.

faust-streaming[prometheus] for using the Prometheus Faust monitor.

Event Loops

faust-streaming[uvloop] for using Faust with uvloop.

faust-streaming[eventlet] for using Faust with eventlet

Debugging

faust-streaming[debug] for using aiomonitor to connect and debug a running Faust worker.

faust-streaming[setproctitle]when the setproctitle module is installed the Faust worker will use it to set a nicer process name in ps/top listings.vAlso installed with the fast and debug bundles.

Downloading and installing from source

Download the latest version of Faust from https://pypi.org/project/faust-streaming/

You can install it by doing:

$ tar xvfz faust-streaming-0.0.0.tar.gz
$ cd faust-streaming-0.0.0
$ python setup.py build
# python setup.py install

The last command must be executed as a privileged user if you are not currently using a virtualenv.

Using the development version

With pip

You can install the latest snapshot of Faust using the following pip command:

pip install https://github.com/faust-streaming/faust/zipball/master#egg=faust

FAQ

Can I use Faust with Django/Flask/etc

Yes! Use eventlet as a bridge to integrate with asyncio.

Using eventlet

This approach works with any blocking Python library that can work with eventlet

Using eventlet requires you to install the faust-aioeventlet module, and you can install this as a bundle along with Faust:

pip install -U faust-streaming[eventlet]

Then to actually use eventlet as the event loop you have to either use the -L <faust --loop> argument to the faust program:

faust -L eventlet -A myproj worker -l info

or add import mode.loop.eventlet at the top of your entry point script:

#!/usr/bin/env python3
import mode.loop.eventlet  # noqa

It's very important this is at the very top of the module, and that it executes before you import libraries.

Can I use Faust with Tornado

Yes! Use the tornado.platform.asyncio bridge

Can I use Faust with Twisted

Yes! Use the asyncio reactor implementation: https://twistedmatrix.com/documents/current/api/twisted.internet.asyncioreactor.html

Will you support Python 2.7 or Python 3.5

No. Faust requires Python 3.8 or later, since it heavily uses features that were introduced in Python 3.6 (async, await, variable type annotations).

I get a maximum number of open files exceeded error by RocksDB when running a Faust app locally. How can I fix this

You may need to increase the limit for the maximum number of open files. On macOS and Linux you can use:

ulimit -n max_open_files to increase the open files limit to max_open_files.

On docker, you can use the --ulimit flag:

docker run --ulimit nofile=50000:100000 <image-tag> where 50000 is the soft limit, and 100000 is the hard limit See the difference.

What kafka versions faust supports

Faust supports kafka with version >= 0.10.

Getting Help

Slack

For discussions about the usage, development, and future of Faust, please join the fauststream Slack.

Resources

Bug tracker

If you have any suggestions, bug reports, or annoyances please report them to our issue tracker at https://github.com/faust-streaming/faust/issues/

License

This software is licensed under the New BSD License. See the LICENSE file in the top distribution directory for the full license text.

Contributing

Development of Faust happens at GitHub

You're highly encouraged to participate in the development of Faust.

Code of Conduct

Everyone interacting in the project's code bases, issue trackers, chat rooms, and mailing lists is expected to follow the Faust Code of Conduct.

As contributors and maintainers of these projects, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

We are committed to making participation in these projects a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.

Examples of unacceptable behavior by participants include:

  • The use of sexualized language or imagery
  • Personal attacks
  • Trolling or insulting/derogatory comments
  • Public or private harassment
  • Publishing other's private information, such as physical or electronic addresses, without explicit permission
  • Other unethical or unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.

This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

faust-streaming-0.11.3.tar.gz (759.2 kB view details)

Uploaded Source

Built Distributions

faust_streaming-0.11.3-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

faust_streaming-0.11.3-cp311-cp311-macosx_11_0_arm64.whl (514.7 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

faust_streaming-0.11.3-cp311-cp311-macosx_10_9_x86_64.whl (518.1 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

faust_streaming-0.11.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

faust_streaming-0.11.3-cp310-cp310-macosx_11_0_arm64.whl (514.6 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

faust_streaming-0.11.3-cp310-cp310-macosx_10_9_x86_64.whl (517.8 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

faust_streaming-0.11.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

faust_streaming-0.11.3-cp39-cp39-macosx_11_0_arm64.whl (515.4 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

faust_streaming-0.11.3-cp39-cp39-macosx_10_9_x86_64.whl (518.5 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

faust_streaming-0.11.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

faust_streaming-0.11.3-cp38-cp38-macosx_11_0_arm64.whl (516.4 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

faust_streaming-0.11.3-cp38-cp38-macosx_10_9_x86_64.whl (519.8 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

Details for the file faust-streaming-0.11.3.tar.gz.

File metadata

  • Download URL: faust-streaming-0.11.3.tar.gz
  • Upload date:
  • Size: 759.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for faust-streaming-0.11.3.tar.gz
Algorithm Hash digest
SHA256 3488be5dec17dcd4613dfa2600bb2bf83ebadb05de4620dca791ed4a0b7bd650
MD5 25a582f8756808877a763413fb64f454
BLAKE2b-256 84f662d1c034a246178fafaa4aafb51d823a5e3103149120bcec731f0ea0bfdb

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c0eb3438bc05e77446ce5e8c0ed0929d983a3f29e4db12a58c084416041c2aea
MD5 326a1941fd3b945c816c23062cf0d79c
BLAKE2b-256 86d03cef9ab2cbda3223fa920909e1ec135f334283343a0ef42a58cf56080735

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c3b26283253f94b8d6e73f7184e2b91dd33c3661a35ef0111efed6e403f54f54
MD5 dc73469ff30785494d0d266ee7de9d83
BLAKE2b-256 1e5bb2251f781db100676ed5efe79714ccf31acab8c83a19eb91eb431f1d7b27

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 115d274ec3366a2b03ee85fd9b25aeeaa8389ae77dd8d029559a99e8e7a401cb
MD5 18c21e87b60b7657ef023246f9296941
BLAKE2b-256 7145ac219344d5b48ab8dae05a8179dfcf9ca1d62c71d3c658f7b30e4f60d5bf

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c3b112b1642c2ae952600eb1b5d873d1cb66ee568019ddd9ede6bd1e1afb15e3
MD5 8da89c019c3f9b4b71299b8870630fe6
BLAKE2b-256 ad61edb75b1771b36855b955e696dbae2b7929e48ed7156d495a937b1d3130d3

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bd06c9f97727032a90597f92c0f06514286011c47038c1b606005449b0526033
MD5 eb15a013ccb0d1c4c830942aa78357cb
BLAKE2b-256 d675cfab604fb8853e56df1d78c1925770a9145420ba902c8b1528f9337d9873

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ce60311f1130351c86ffe42139462f182ac308b744a37f879e336c31e9661daa
MD5 cd6bfb7ab8d38df28c1ac815dc3b0429
BLAKE2b-256 04c403dea4f5b06a4a13a0d138991605693ac89c562f388f5e8eba834c2ec1bf

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 15f025b695981162cd4c199881bb3b0b9f0fb0e8f42575b87ada61349013055a
MD5 8e0e9b9db4c409154d63da47c3bec027
BLAKE2b-256 961d372eb9391e26da44d3837bcb335f5b66507b0b4b447d9778b3da43e7f32f

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7eace309ba7f164ef6c06bcdcda2e96b87bb658cee652f0732f9f6dc1dafc05f
MD5 864fa26ae22e0c1d53f534eb408144cc
BLAKE2b-256 bf3f102376799aadbe07b0cdfd8aeadee760627d038eb796741ccc3729a16e31

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 53a05931c04a0d6db9e49faeb54afbd5bd25c4d6332eda711aaa9df1acc8446f
MD5 c1b3c56bd94ccf04b3add55ecb33c990
BLAKE2b-256 4ee035b5b5b40e2ef54790222548ff28159c54a2c9839a8e8368560816b7fbf3

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 989e88c8e7a267728dd20c671b5834e3bfbd67867dd32dfd7008819a3c3c011e
MD5 fad66f230d59e68c373d6d26823f5350
BLAKE2b-256 9e1e391094c27be4bbc9bcc8a6bca54d8b241d2a9677d4be7fa385b8ee528197

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e1bb745e461ea1ff5125ff57ca3d83618b7f4fbb108dea4308ef081294ee04d
MD5 f22dc049bc50e9567410593928c8236a
BLAKE2b-256 5c5c1d389cf5657388da94193d9e9ea992c444228bd1a9ae892c52bc9963ceb0

See more details on using hashes here.

File details

Details for the file faust_streaming-0.11.3-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for faust_streaming-0.11.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5b28eaa52f2b4ee131e8a36f451159d2d255d7583439b642e932084061b22e96
MD5 46a3433df3436f79e6a2cbfafe853d27
BLAKE2b-256 ea80483ba332fdbb5ae2f3200f1449035c41fbbb07e6eeb3443af4627643c63f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page