Skip to content

Add Named Environment API#46

Open
benoitc wants to merge 2 commits intomainfrom
feature/exec-all
Open

Add Named Environment API#46
benoitc wants to merge 2 commits intomainfrom
feature/exec-all

Conversation

@benoitc
Copy link
Owner

@benoitc benoitc commented Mar 21, 2026

Summary

  • Add py:new_env/1,2 to create reusable Python environments with initialization code
  • Named environments are stored in persistent_term for global lookup
  • Add py:set_env/1, py:get_env/0,1, py:list_envs/0, py:destroy_env/1
  • Update py:eval/exec and py_event_loop_pool:eval/exec to use named env when set
  • Remove preload API from py_event_loop_pool in favor of named environments

Example usage:

{ok, _} = py:new_env(<<"import numpy as np">>, #{name => numpy_env}),
ok = py:set_env(numpy_env),
{ok, Result} = py:eval(<<"np.array([1,2,3])">>).

Executes Python code on all workers in the pool. Useful for
initializing shared state, importing modules, or defining functions
across all workers.
@benoitc benoitc force-pushed the feature/exec-all branch 2 times, most recently from e600013 to 355b75a Compare March 21, 2026 06:50
Add py:new_env/1,2 to create reusable Python environments with
initialization code. Environments can be named for global lookup
via persistent_term.

New API:
- py:new_env(Code) / py:new_env(Code, #{name => atom()})
- py:set_env(Name | Ref) - set current env for process
- py:get_env() / py:get_env(Name) - get current/named env
- py:list_envs() - list all named environments
- py:destroy_env(Ref) - remove from registry

When a named env is set, py:eval/exec and py_event_loop_pool:eval/exec
use it directly via NIF instead of routing through contexts.

Remove preload API from py_event_loop_pool in favor of named
environments which provide more flexibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant