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

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement