Mixing Python with Elixir II

Badu
HackerNoon.com
5 min readNov 17, 2017

--

Asynchronous communication

Important: Read Part I of this post before continuing.

In Part I, we looked at how to call Python functions from Elixir using Erlport!

ErlPort is an Elixir library which makes it easier to connect Erlang to a number of other programming languages using the Erlang port protocol.

However, as you may have noticed, the calling process had to wait for Python to finish processing and return the results. This works for operations that are instant and doesn’t take much time. Otherwise, it’s not much fun when you are running an intensive Python function. Because your Elixir program is gonna hang waiting for the Python code to finish.

For operations that take a while to complete we need a way to call the Python function and get notified in our elixir code when the function is complete — similar to how we write elixir program with processes talking to each asynchronously. This is useful, especially in cases when you want an Elixir process to control/supervise several Python operations. We can run the Python functions concurrently which makes life a little better.

In this post, we’ll explore how to achieve this — Asynchronous communication between Python and Elixir!

We can reason about the challenge the same way we do in communicating between Elixir processes — by using cast — which allows us to communicate between processes asynchronously.

Previously, we used erlport’s :python.call/4 mostly when calling functions from Python. This is synchronous and the elixir program had to wait for the python function to finish and send response.

In this post, we will use :python.cast/2

Understanding :python.cast/2

:python.cast/2 works the same as GenServer.cast/2. You give it process id (or registered process name) and the message to send to that process. Unlike :python.call variant, :python.cast is used to send message instead of calling a Python function. Same way your Elixir processes are sent messages. Erlport provides a mechanism for us to receive messages sent to the python instance.

Erlport Python Module

Erlport offers a python library that helps with working with elixir data types and processes. You can install erlport using pip.

However, the Erlport Elixir repo already comes with the Python library which are automatically loaded at runtime. So you don’t have to install the Erlport Python library if you are using Erlport through Elixir.

Below is how data is mapped between Erlang/Elixir and Python

Erlang/Elixir data types mapped to Python data types (http://erlport.org/docs/python.html)
Python data types mapped to Erlang/Elixir data types (http://erlport.org/docs/python.html)

Handling Message Sent from Elixir to Python

For Elixir processes you have a receive block in which all messages are handled. Or the handle_call, handle_cast and handle_info functions if you are using GenServer. Erlport provides a similar mechanism for handling messages sent to the Python process. set_message_handler is the function. It’s in the erlport.erlang module. It takes a Python function with one argument-message i.e the incoming message. Erlport will then call the function passed to set_message_handler whenever a message is received. It’s like a callback function.

Sending Message from Python to Elixir

Since an Elixir process isn’t aware of who sent a cast message, we have to find a way to know which process to send the result to once the python function completes. One way is to register the process id, of the Elixir process that you want the result sent to.

The Erlport python module provides cast function for sending a message to Elixir process. With this we can send asynchronously message back to the elixir process when the function is done!

Putting it all together

Open Terminal and create a new Elixir project using mix

Add dependency

Add erlport to your dependencies mix.exs

Then install project dependencies.

Create priv/python directory where you’ll keep our python modules

Create an elixir module wrapper for Erlport related functions.

In order for Elixir process to receive message asynchronously, we will create a simple GenServer module

Now lets create the python module priv/python/test.py

Compile!

Run!

Also notice that call_count causes iex to hang till we get the back result from python

Now let’s call same python function asynchronously!

Notice how we can continue to work in iex and get the result once our python function completes. Try callingcast_count with different numbers — large and small.

That is it! Now you can communicate between Elixir and Python asynchronously.

Happy coding!

--

--