4141 call /5 ,
4242 cast /3 ,
4343 cast /4 ,
44+ cast /5 ,
45+ spawn_call /3 ,
46+ spawn_call /4 ,
47+ spawn_call /5 ,
4448 await /1 ,
4549 await /2 ,
4650 eval /1 ,
@@ -270,16 +274,42 @@ exec(Ctx, Code) when is_pid(Ctx) ->
270274% %% Asynchronous API
271275% %% ============================================================================
272276
273- % % @doc Cast a Python function call, returns immediately with a ref.
274- % % The call executes in a spawned process. Use await/1,2 to get the result.
275- -spec cast (py_module (), py_func (), py_args ()) -> py_ref ().
277+ % % @doc Fire-and-forget Python function call.
278+ -spec cast (py_module (), py_func (), py_args ()) -> ok .
276279cast (Module , Func , Args ) ->
277280 cast (Module , Func , Args , #{}).
278281
279- % % @doc Cast a Python function call with kwargs.
280- -spec cast (py_module (), py_func (), py_args (), py_kwargs ()) -> py_ref ().
282+ % % @doc Fire-and-forget Python function call with context or kwargs.
283+ -spec cast (pid (), py_module (), py_func (), py_args ()) -> ok ;
284+ (py_module (), py_func (), py_args (), py_kwargs ()) -> ok .
285+ cast (Ctx , Module , Func , Args ) when is_pid (Ctx ) ->
286+ cast (Ctx , Module , Func , Args , #{});
281287cast (Module , Func , Args , Kwargs ) ->
282- % % Spawn a process to execute the call and return a ref
288+ spawn (fun () ->
289+ Ctx = py_context_router :get_context (),
290+ _ = py_context :call (Ctx , Module , Func , Args , Kwargs )
291+ end ),
292+ ok .
293+
294+ % % @doc Fire-and-forget Python function call with context and kwargs.
295+ -spec cast (pid (), py_module (), py_func (), py_args (), py_kwargs ()) -> ok .
296+ cast (Ctx , Module , Func , Args , Kwargs ) when is_pid (Ctx ) ->
297+ spawn (fun () ->
298+ _ = py_context :call (Ctx , Module , Func , Args , Kwargs )
299+ end ),
300+ ok .
301+
302+ % % @doc Spawn a Python function call, returns immediately with a ref.
303+ -spec spawn_call (py_module (), py_func (), py_args ()) -> py_ref ().
304+ spawn_call (Module , Func , Args ) ->
305+ spawn_call (Module , Func , Args , #{}).
306+
307+ % % @doc Spawn a Python function call with context or kwargs.
308+ -spec spawn_call (pid (), py_module (), py_func (), py_args ()) -> py_ref ();
309+ (py_module (), py_func (), py_args (), py_kwargs ()) -> py_ref ().
310+ spawn_call (Ctx , Module , Func , Args ) when is_pid (Ctx ) ->
311+ spawn_call (Ctx , Module , Func , Args , #{});
312+ spawn_call (Module , Func , Args , Kwargs ) ->
283313 Ref = make_ref (),
284314 Parent = self (),
285315 spawn (fun () ->
@@ -289,6 +319,17 @@ cast(Module, Func, Args, Kwargs) ->
289319 end ),
290320 Ref .
291321
322+ % % @doc Spawn a Python function call with context and kwargs.
323+ -spec spawn_call (pid (), py_module (), py_func (), py_args (), py_kwargs ()) -> py_ref ().
324+ spawn_call (Ctx , Module , Func , Args , Kwargs ) when is_pid (Ctx ) ->
325+ Ref = make_ref (),
326+ Parent = self (),
327+ spawn (fun () ->
328+ Result = py_context :call (Ctx , Module , Func , Args , Kwargs ),
329+ Parent ! {py_response , Ref , Result }
330+ end ),
331+ Ref .
332+
292333% % @doc Wait for an async call to complete.
293334-spec await (py_ref ()) -> py_result ().
294335await (Ref ) ->
0 commit comments