"Hello, World! 🎉" in Python


Rust Python Node Java

In this tutorial, you will learn how to create a topic, build a producer/consumer in Python, and sends a “Hello, World! 🎉” message.

Prerequisites 

Before starting on this tutorial, you’ll need to have completed the following

  • Install Python (3.6 or above)
  • Have the Fluvio CLI (version 0.7.1 or greater) installed [1]
  • Have access to a Fluvio cluster.

See our getting started guide for more details on getting set up.

Create a Topic using the Fluvio CLI 

In Fluvio, we send all of our messages to something called a Topic, which is like a category for related messages. For this tutorial, we’ll create a topic called hello-python using the following command:

$ fluvio topic create hello-python

Check Python 

A Fluvio environment for Python requires Python 3.6 or above.

Install Python 

Python installation varies depending on your operating system.

Operating System Instructions
MacOS There’s an official installer from python.org to install on macOS but can also download python via brew.
Linux There’s an official installer from python.org. It’s also available in nearly every package manager. This guide helps with ubuntu.

To check your python version run the command python --version.

Writing the Application 

The following sections will setup your project and walk through writing the application files.

Installing Project Dependencies 

Run the following script to setup your project for development:

$ mkdir fluvio-python-demo
$ cd fluvio-python-demo
$ python -m venv venv
$ source venv/bin/activate
$ pip install fluvio
$ touch producer.py consumer.py

This willl create a directory fluvio-python-demo with a Virtual Environment in it in the venv sub directory. This will then install the fluvio package from pypi.org in the virtual environment.

Your working directory should now contain the following files:

$ ls
consumer.py  producer.py  venv

Writing the producer.py File 

Write the following code in your producer.py file.

from fluvio import Fluvio
fluvio = Fluvio.connect()

producer = fluvio.topic_producer("hello-python")
partition = 0
while True:
    line = input('> ')
    producer.send_record_string(line, partition)
This code performs the following actions:
  • Import fluvio;
  • Create a new Fluvio Client Instance;
  • Create a connection to a local Fluvio Cluster;
  • Create a new topic producer for hello-python;
  • Listen for input typed into the terminal;
  • Send typed input to the fluvio cluster;

Writing the consumer.py File 

Write the following code in your consumer.py file.

from fluvio import (Fluvio, Offset)
fluvio = Fluvio.connect()
partition = 0
consumer = fluvio.partition_consumer("hello-python", partition)
for i in consumer.stream(Offset.beginning()):
    print("Received message: %s" % i.value_string())
This code performs the following actions:
  • Import fluvio module;
  • Create a new Fluvio Client Instance;
  • Create a connection to a local Fluvio Cluster;
  • Create a new topic consumer for hello-python;
  • Listen for events sent by a topic producer;

Running the Demo 

Now that the code is written, we’re ready to run our Hello, World! 🎉 example. Run the following commands in separate terminals.

Running the Producer 

Run the following command in the working directory:

$ ./venv/bin/python ./producer.py
Fluvio Producer created, waiting for input:

>

Great! Now type Hello, World! 🎉 into your terminal window:

Hello, World! 🎉


Running the Consumer 

Open a new terminal and run the following command:

$ ./venv/bin/python ./consumer.py
Fluvio Consumer created, listening for events:


Received message: Hello, World! 🎉

Congratulations! 

You’ve now completed the Fluvio “Hello, World! 🎉” tutorial.

Head over to the Fluvio Python documentation to learn more about the library and available options.

Read the fluvio Docs 

Check out Python API reference guide for additional usage information and documentation.