Ich sende meinen JSON erfolgreich in die Python, läuft es und erhalte die Ergebnisse, aber ich erhalte das Ergebnis, wenn ich es von LiveView ausführt (wenn ich meine Python Mynnally im Terminal leite, wird die Ergebnisse, die JSON gut zurückgegeben wird). Es sollte eine einfache Sache sein, ich bin mir sicher, dass ich einen wirklich einfachen Punkt fehlt. Kann jemand helfen? Fyi, mein Python -Geschäftsprozess dauert 0,03 Sekunden, wenn man von Anfang bis Ende manuell ausgeführt wird.
Code: Select all
def main():
try:
raw_input = sys.stdin.read().strip()
input_data = json.loads(raw_input)
results = optimizer_function(input_data) # optimizer_function is my business process
output = json.dumps(results)
sys.stdout.write(output + "\n")
sys.stdout.flush()
except Exception as e:
sys.stdout.write(json.dumps({"error": str(e)}) + "\n")
sys.stdout.flush()
if __name__ == "__main__":
main()
< /code>
Mein Phoenix Optimizer Business Process Code Snippets: < /p>
defmodule Commrefiner.Optimizer do
def run_optimizer(params) do
json_input = Jason.encode!(params)
script_path = Application.fetch_env!(:commrefiner, :optimizer_script_path)
port = Port.open({:spawn_executable, "path/to/virtual/environment/python3"}, [
{:args, [script_path]},
:binary,
:exit_status,
:use_stdio,
:stderr_to_stdout
])
Port.command(port, json_input "\n")
response = collect_output(port, "")
Port.close(port)
case Jason.decode(response) do
{:ok, result} -> result
{:error, _} -> %{"error" => "Invalid JSON response from optimizer"}
end
end
defp collect_output(port, acc) do
case Port.info(port, :os_pid) do
{:os_pid, _pid} ->
receive do
{^port, {:data, output}} -> collect_output(port, acc output)
{^port, {:exit_status, status}} -> acc
after
15_000 -> # ✅ Allow enough time for Python output
acc
end
nil ->
acc
end
end
end
< /code>
Mein Phoenix Live_view Code Snippets: < /p>
def handle_event("run_optimizer", _params, socket) do
params = %{params: socket.assigns.user_inputs}
headers = [{"Content-Type", "application/json"}] # Define and ensure correct headers
# encode JSON before sending
response = HTTPoison.post("http://localhost:4000/api/optimizer", Jason.encode!(params), headers)
case response do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
case Jason.decode(body) do
{:ok, results} -> {:noreply, assign(socket, results: results)}
{:error, _} -> {:noreply, assign(socket, results: %{"error" => "Invalid JSON response"})}
end
{:ok, %HTTPoison.Response{status_code: code, body: body}} -> # ✅ Handle non-200 responses
IO.puts("❌ API returned status #{code}")
IO.inspect(body, label: "Non-200 Response")
{:noreply, assign(socket, results: %{"error" => "API error: #{code}"})}
{:error, %HTTPoison.Error{reason: reason}} -> # ✅ Handle request errors
IO.puts("❌ Optimizer API request failed")
IO.inspect(reason, label: "Optimizer API Error")
{:noreply, assign(socket, results: %{"error" => "Failed to call optimizer"})}
end
end
Timeout in der Phoenix -API -Anforderung:
[*] Optimierer -API -Fehler :: Timeout < /li>
Python -Prozess zeitlich festgelegt. Partielle Daten: ""
Python -Skript sendet keine Ausgabe vor der Zeitüberschreitung:
[*] Vollständige rohe Antwort von Python: ""
RAW -Pythonausgabe: ""
Was passiert? li>
Es sendet die JSON -Eingabe. Bevor Python Daten zurückgeben kann.>