modal.Function

Functions are the basic units of serverless execution on Modal.

Generally, you will not construct a Function directly. Instead, use the App.function() decorator to register your Python functions with your App.

hydrate 

Synchronize the local object with its identity on the Modal server.

It is rarely necessary to call this method explicitly, as most operations will lazily hydrate when needed. The main use case is when you need to access object metadata, such as its ID.

Added in v0.72.39: This method replaces the deprecated .resolve() method.

update_autoscaler 

Override the current autoscaler behavior for this Function.

Unspecified parameters will retain their current value, i.e. either the static value from the function decorator, or an override value from a previous call to this method.

Subsequent deployments of the App containing this Function will reset the autoscaler back to its static configuration.

Examples:

from_name 

Reference a Function from a deployed App by its name.

This is a lazy method that defers hydrating the local object with metadata from Modal servers until the first time it is actually used.

get_web_url 

URL of a Function running as a web endpoint.

remote 

Calls the function remotely, executing it with the given arguments and returning the execution’s result.

remote_gen 

Calls the generator remotely, executing it with the given arguments and returning the execution’s result.

local 

Calls the function locally, executing it with the given arguments and returning the execution’s result.

The function will execute in the same environment as the caller, just like calling the underlying function directly in Python. In particular, only secrets available in the caller environment will be available through environment variables.

spawn 

Calls the function with the given arguments, without waiting for the results.

Returns a modal.FunctionCall object that can later be polled or waited for using .get(timeout=...). Conceptually similar to multiprocessing.pool.apply_async, or a Future/Promise in other contexts.

get_raw_f 

Return the inner Python object wrapped by this Modal Function.

get_current_stats 

Return a FunctionStats object describing the current function’s queue and runner counts.

map 

Parallel map over a set of inputs.

Takes one iterator argument per argument in the function being mapped over.

Example:

If applied to a app.function, map() returns one result per input and the output order is guaranteed to be the same as the input order. Set order_outputs=False to return results in the order that they are completed instead.

return_exceptions can be used to treat exceptions as successful results:

starmap 

Like map, but spreads arguments over multiple function arguments.

Assumes every input is a sequence (e.g. a tuple).

Example:

for_each 

Execute function for all inputs, ignoring outputs. Waits for completion of the inputs.

Convenient alias for .map() in cases where the function just needs to be called. as the caller doesn’t have to consume the generator to process the inputs.

spawn_map 

Spawn parallel execution over a set of inputs, exiting as soon as the inputs are created (without waiting for the map to complete).

Takes one iterator argument per argument in the function being mapped over.

Example:

Programmatic retrieval of results will be supported in a future update.