Current section
Files
Jump to
Current section
Files
lib/python_interface/application.ex
defmodule PythonInterface.Application do
@moduledoc false
use Application
@impl true
def start(_type, _args) do
enable_sigchld()
children = [
PythonInterface.Janitor
]
opts = [strategy: :one_for_one, name: PythonInterface.Supervisor]
with {:ok, result} <- Supervisor.start_link(children, opts) do
maybe_uv_init()
{:ok, result}
end
end
# If configured, we fetch Python and dependencies at compile time
# and we automatically initialize the interpreter on boot.
if pyproject_toml = Application.compile_env(:python_interface, :uv_init)[:pyproject_toml] do
PythonInterface.Uv.fetch(pyproject_toml, true)
defp maybe_uv_init(), do: PythonInterface.Uv.init(unquote(pyproject_toml), true)
else
defp maybe_uv_init(), do: :noop
end
defp enable_sigchld() do
# Some APIs in Python, such as subprocess.run, wait for a child
# OS process to finish. On Unix, this relies on `waitpid` C API,
# which does not work properly if SIGCHLD is ignored, resulting
# in infinite waiting. ERTS ignores the signal by default, so we
# explicitly restore the default handler.
case :os.type() do
{:win32, _osname} -> :ok
{:unix, _osname} -> :os.set_signal(:sigchld, :default)
end
end
end