Skip to content

Add Named Environment API#46

Closed
benoitc wants to merge 1 commit intomainfrom
feature/exec-all
Closed

Add Named Environment API#46
benoitc wants to merge 1 commit 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])">>).

@benoitc benoitc force-pushed the feature/exec-all branch 3 times, most recently from 355b75a to 4c9ff1a Compare March 21, 2026 06:53
Adds explicit import caching for Python modules and functions:

API:
- py:import/1 - Import and cache a module
- py:import/2 - Import module and cache a specific function
- py:flush_imports/0 - Clear all import caches
- py:import_stats/0 - Get cache entry count
- py:import_list/0 - List cached imports as #{Module => [Funcs]}

Key behaviors:
- __main__ is never cached (returns error)
- Caching is per-process namespace (via PID affinity)
- All Python execution commands benefit from the cache

Tests:
- py_import_SUITE: 12 tests for basic import functionality
- py_import_owngil_SUITE: 9 tests for OWN_GIL mode (Python 3.12+)
- Multi-process and concurrent stress tests included
@benoitc benoitc closed this Mar 22, 2026
@benoitc benoitc deleted the feature/exec-all branch March 22, 2026 16:12
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