Maxximiliann

Maxximiliann

Diagnosing port communications issue

defmodule PythonProcessAPI do
  require Logger

  def process_output(foo, _, python_path) do
    Print.text("Results received from #{python_path}.")

    Logger.info("#{inspect(foo)}, __MODULE__, line 8")
  end

  def connect(data, python_path) do
    port =
      :erlang.open_port(
        {:spawn, python_path},
        [:binary, packet: 4]
      )

    execute = fn input ->
      send(port, {self(), {:command, :erlang.term_to_binary({input})}})

      receive do
        foo ->
          process_output(foo, data, python_path)
      end
    end

    execute.(data)
  end
end
#my_python_process.py

import sys
import simplejson as json
from loguru import logger
from conversion import to_string
from erlastic import port_connection, Atom as atom
from some_library.errors import (BaseError, AuthenticationError, PermissionDenied,
                                ArgumentsRequired, BadRequest, BadResponse,
                                NullResponse, InvalidAddress, NotSupported, NetworkError, DDoSProtection,
                                RateLimitExceeded, OnMaintenance, InvalidNonce, RequestTimeout)


mailbox, port = port_connection()


@logger.catch
def submit_report(success_status, report, arg1, arg2, arg3):
    logger.info("Processing report . . . ")
    full_report = report | {
        'arg1': arg1, 'arg2': arg2, 'arg3': arg3}
    full_report_json = json.dumps(full_report)

    try:
        port.send((atom(success_status), (full_report_json)))
        logger.info("Transmitted report . . . ")
    except (BrokenPipeError) as error:
        logger.exception(error)
        sys.exit()
    finally:
        sys.exit()


@logger.catch
def fetch_params():
    for params_data in mailbox:
        params_hash = to_string(params_data)
        return params_hash[0]


@logger.catch
def some_function():
    params_json = fetch_params()
    params = json.loads(params_json)
   
   arg1, arg2, arg3 = params

    try:
        submission_report = some_operation(params)

    except (BaseError, AuthenticationError, PermissionDenied,
            ArgumentsRequired, BadRequest, BadResponse,
            NullResponse, InvalidAddress, NotSupported, NetworkError, DDoSProtection,
            RateLimitExceeded, OnMaintenance, InvalidNonce, RequestTimeout) as error:

        logger.exception(error)
        logger.info(params)
        submit_report(
            'error', {'error': str(error)}, arg1, arg2, arg3)
    except Exception as crash_report:
        logger.exception(crash_report)
        logger.info(params)
        submit_report('error', {'error': str(
            crash_report)}, arg1, arg2, arg3)
    else:
        submit_report(
            'ok', submission_report, arg1, arg2, arg3)


@ logger.catch
def main():
    some_function()


if __name__ == '__main__':
    main()

The problem we’re having is that data can be passed to the Python process successfully but the return message never reaches the Elixir side. All success messages from the Python end are logged, but there are no exceptions or error messages to go on. We’re at a loss for how to diagnose what’s wrong. Suggestions? Recommendations?

Most Liked

kokolegorille

kokolegorille

Can’t comment on erlastic, but I have been using erlport lately with python.

This blog post is really nice…

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement