Alex Rozumii, Toptal
Elixir Club, 28 January 2017
What's up, doc?
require "erlix"
Erlix::Node.init("ruby",nil)
c=Erlix::Connection.new("foo@kdr2-pc")
p=Erlix::Pid.new(c)
# {Pid, :test_atom}
c.esend("my_pid", Erlix::Tuple.new([p, Erlix::Atom.new("test_atom")]))
defmodule SomePythonCall do
use Export.Python
def call_python_method
# path to our python files
{:ok, py} = Python.start(python_path: Path.expand("lib/python"))
# call "upcase" method from "test" file with "hello" argument
py |> Python.call("test", "upcase", ["hello"])
# same as above but prettier
py |> Python.call(upcase("hello"), from_file: "test")
end
end
*according to poll, 2014
>>> import redis
>>> r = redis.StrictRedis()
>>> lua = """
... local res = {}
... for i = 2, #ARGV do
... table.insert(res,
redis.call('publish', ARGV[i], ARGV[1]))
... end
... return res
... """
>>> multipublish = r.register_script(lua)
>>> multipublish(args=["message", "channel1",
"channel2", "channel3"])
[1L, 0L, 0L]
worker(CitiesInsertListener, [Application.get_env(:app, Repo)]),
defmodule CitiesInsertListener do
use Telepathy.Listener, table_name: "cities"
def handle_insert(new, state) do
CitiesListenerAgent.push_message(__MODULE__, new)
{:noreply, state}
end
end
CREATE OR REPLACE FUNCTION <%= @function_name %>()
RETURNS trigger AS $$
BEGIN
CASE TG_OP
WHEN 'INSERT' THEN
PERFORM pg_notify(
'<%= @channel_name %>',
json_build_object(
'table', TG_TABLE_NAME,
'type', TG_OP,
'old_data', NULL,
'new_data', row_to_json(NEW)
)::text
);
{:ok, notif_pid} = Postgrex.Notifications.start_link(db_connection)
Postgrex.Notifications.listen(notif_pid, "cities")
{:ok, %{notif_pid: notif_pid, pg_pid: pg_pid}}
...
def handle_info(raw_msg, state) do
msg = Poison.decode! elem(raw_msg, 4)
...
Alex Rozumii
@brain-geek
alex@rozumiy.name