Comment on page
A hello world program for the Grainite Python API.
This example app shows you how events are sent to a topic and processed with action handlers, as well as how to send further events to other topics in response. All of the code shown below has already been implemented for you in
samples/helloPython. It assumes familiarity with Grainite concepts and is mainly intended to introduce Python developers to the Grainite Python API.
Loading and running the application is as easy as running a couple of commands:
# Load the application on Grainite
# Note: GX is an alias set to samples/gx
# Send work to the application
After loading an app with
gx load, you can run
gx app lsto verify that your app has been loaded. You can also remove any apps currently loaded on the server using
gx app remove -a <app name>
You should see that a few events were sent by the client and processed by the application running in Grainite. Read on to understand how the app works.
To see what happened after you sent work to the app, you can look at a few things:
The following will print the application logs:
gx log --follow
You can access the logger using
context.get_logger()in your app's source code to print your own log lines (see for example
Run the below command.
gxwill then ask you to select a topic in the application and will print the events which were sent to that topic.
gx topic dump
Run the below command.
gxwill then ask you to select a table in the application and will print the grains in that table.
gx table dump
The YAML file for Python apps in Grainite are largely the same as a YAML file for a Java application. However, we need to specify the language
typeand the for our action handlers as
pythonand set their
script_pathto the location of the Python file containing the source code for the action handler. We also need to provide the path to the directory containing the app's source Python files under
- table_name: person_table
- name: GreetingHandler
- action_name: handleGreeting
- subscription_name: peopleEvents
- topic_name: people
- topic_name: greetings
The application client has been implemented in
The application has a single action handler, which has been implemented in
You may refer to the inline comments these two Python files to understand how the Grainite Python API is used by the application.
samples/helloPython/src/Client.pyshows how events are sent to a topic by the client in Python, while
samples/helloPython/src/actions/GreetingHandler.pyshows how the events are processed with action handlers.
gx gen, which is used to auto-generate the action handler and client source code files given an app YAML file, is only supported for Java apps. The source code files for Python apps, like the two specified above, currently need to be created manually after creating the app YAML file.