Skip to content

Add automatic env reuse for py_event_loop:run#33

Merged
benoitc merged 1 commit intomainfrom
feature/env-reuse-event-loop
Mar 16, 2026
Merged

Add automatic env reuse for py_event_loop:run#33
benoitc merged 1 commit intomainfrom
feature/env-reuse-event-loop

Conversation

@benoitc
Copy link
Owner

@benoitc benoitc commented Mar 16, 2026

Summary

  • Functions defined via py:exec(Ctx, Code) can now be called via py_event_loop:run without manual env passing
  • The process-local env is automatically detected and passed to the NIF
  • Supports both sync and async functions defined in the process namespace

Example:

Ctx = py:context(1),
ok = py:exec(Ctx, <<"
async def async_add(a, b):
    await asyncio.sleep(0.01)
    return a + b
">>),

%% Just works - env is reused automatically
{ok, 42} = py_event_loop:run('__main__', async_add, [10, 32]).

@benoitc benoitc force-pushed the feature/env-reuse-event-loop branch 2 times, most recently from bff3aa1 to 2f73bc2 Compare March 16, 2026 20:27
When a process has a local Python environment (from py:context/py:exec),
py_event_loop functions now automatically pass that env to the NIF. This
allows functions defined via py:exec to be called directly without
manual env parameter passing.

Key implementation detail: env resources are stored in a PID->env mapping
(not serialized through the task queue) to avoid memory safety issues with
resource serialization across NIF environments.

Changes:
- Add pid_env_mapping_t struct for storing PID->env mappings
- Add register_pid_env/lookup_pid_env helper functions
- Modify nif_submit_task_with_env to register env by PID
- Modify process_ready_tasks to look up env by caller PID
- Move py_env_resource_t definition to py_nif.h for shared access
- Update py_event_loop:create_task to auto-detect and pass process env
- Update py_event_loop:spawn_task to use caller's env
- Add tests for env reuse with sync, async, and spawn_task functions
@benoitc benoitc force-pushed the feature/env-reuse-event-loop branch from 2f73bc2 to 90936e1 Compare March 16, 2026 20:59
@benoitc benoitc merged commit 1ae7f62 into main Mar 16, 2026
10 of 11 checks passed
@benoitc benoitc deleted the feature/env-reuse-event-loop branch March 16, 2026 21:11
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