|
38 | 38 |
|
39 | 39 | #include "py_nif.h" |
40 | 40 | #include "py_asgi.h" |
| 41 | +#include "py_wsgi.h" |
41 | 42 |
|
42 | 43 | /* ============================================================================ |
43 | 44 | * Global state definitions |
@@ -132,6 +133,7 @@ static ERL_NIF_TERM build_suspended_result(ErlNifEnv *env, suspended_state_t *su |
132 | 133 | #include "py_thread_worker.c" |
133 | 134 | #include "py_event_loop.c" |
134 | 135 | #include "py_asgi.c" |
| 136 | +#include "py_wsgi.c" |
135 | 137 |
|
136 | 138 | /* ============================================================================ |
137 | 139 | * Resource callbacks |
@@ -373,6 +375,13 @@ static ERL_NIF_TERM nif_py_init(ErlNifEnv *env, int argc, const ERL_NIF_TERM arg |
373 | 375 | return make_error(env, "asgi_scope_init_failed"); |
374 | 376 | } |
375 | 377 |
|
| 378 | + /* Initialize WSGI scope key cache for optimized marshalling */ |
| 379 | + if (wsgi_scope_init() < 0) { |
| 380 | + Py_Finalize(); |
| 381 | + g_python_initialized = false; |
| 382 | + return make_error(env, "wsgi_scope_init_failed"); |
| 383 | + } |
| 384 | + |
376 | 385 | /* Create a default event loop so Python asyncio always has one available */ |
377 | 386 | if (create_default_event_loop(env) < 0) { |
378 | 387 | Py_Finalize(); |
@@ -451,9 +460,10 @@ static ERL_NIF_TERM nif_finalize(ErlNifEnv *env, int argc, const ERL_NIF_TERM ar |
451 | 460 | /* Clean up thread worker system */ |
452 | 461 | thread_worker_cleanup(); |
453 | 462 |
|
454 | | - /* Clean up ASGI scope key cache */ |
| 463 | + /* Clean up ASGI and WSGI scope key caches */ |
455 | 464 | PyGILState_STATE gstate = PyGILState_Ensure(); |
456 | 465 | asgi_scope_cleanup(); |
| 466 | + wsgi_scope_cleanup(); |
457 | 467 | PyGILState_Release(gstate); |
458 | 468 |
|
459 | 469 | /* Stop executors based on mode */ |
@@ -1870,7 +1880,10 @@ static ErlNifFunc nif_funcs[] = { |
1870 | 1880 |
|
1871 | 1881 | /* ASGI optimizations */ |
1872 | 1882 | {"asgi_build_scope", 1, nif_asgi_build_scope, ERL_NIF_DIRTY_JOB_IO_BOUND}, |
1873 | | - {"asgi_run", 5, nif_asgi_run, ERL_NIF_DIRTY_JOB_IO_BOUND} |
| 1883 | + {"asgi_run", 5, nif_asgi_run, ERL_NIF_DIRTY_JOB_IO_BOUND}, |
| 1884 | + |
| 1885 | + /* WSGI optimizations */ |
| 1886 | + {"wsgi_run", 4, nif_wsgi_run, ERL_NIF_DIRTY_JOB_IO_BOUND} |
1874 | 1887 | }; |
1875 | 1888 |
|
1876 | 1889 | ERL_NIF_INIT(py_nif, nif_funcs, load, NULL, upgrade, unload) |
0 commit comments