Free-threaded Python for UDFs and procedures¶
A free-threaded Python runtime is a build of Python in which the Global Interpreter Lock (GIL) is removed and threads run in parallel. Snowflake supports free-threaded runtimes for Python UDFs, UDTFs, UDAFs, and stored procedures, alongside the standard Python runtimes. You choose the runtime for each function or procedure.
Free-threaded runtime versions end in t, such as 3.14t. For the available versions, see the RUNTIME_VERSION
parameter in CREATE FUNCTION.
For background on free threading in Python, see Python support for free threading in the Python documentation and the community Python Free-Threading Guide.
How a free-threaded runtime runs your code¶
On the standard Python runtimes, Snowflake runs each parallel instance of a function in its own worker process, which gives every instance an isolated Python environment.
On a free-threaded runtime, Snowflake runs your Python workers as threads in one shared Python process:
- Packages are imported once and shared.
- Module-level state, such as a model or lookup table that you load at module scope, is loaded once and shared.
- Threads run in parallel across the available cores.
- Threads that you create in your handler, for example with
concurrent.futures.ThreadPoolExecutor, also run in parallel.
Because workers and functions in the same query share one Python process, they also share module-level state. For more information, see Shared state and thread safety.
Because the whole query already runs in one process, the @udf_init_once decorator has no effect on a
free-threaded runtime. You can leave it in your code. For more information, see
Initializing shared state once in Python UDFs.
Choosing between a standard and a free-threaded runtime¶
Use a free-threaded runtime when:
- Your stored procedures use threads for CPU-bound parallel work.
- Your UDFs load large models, embeddings, or reference data and only read them.
- Your queries call many Python functions, or your functions import large libraries.
- Memory is the limiting factor for your Python workload.
Use a standard runtime when:
- You want existing functions and procedures to keep their current behavior without changes.
- A dependency with native code doesn’t support free threading yet, or you use packages from the Snowflake Anaconda channel.
- Your function needs packages, and your account doesn’t allow access to Artifact Repository.
- Your handler relies on mutable module-level state, such as caches, counters, or global settings.
- You need a feature that free-threaded runtimes don’t support. See Limitations.
Changing RUNTIME_VERSION to a free-threaded version, for example from '3.14' to '3.14t', changes how your
code runs. Before you switch, test the function on both runtimes and compare the results.
Creating a function or procedure that uses a free-threaded runtime¶
Set RUNTIME_VERSION to a free-threaded version. If your handler needs packages, install them through Artifact
Repository, from the shared PyPI repository or a customer-hosted repository.
The following example creates a UDF that uses the 3.14t runtime:
The following example creates a stored procedure that runs CPU-bound work in parallel with
concurrent.futures.ThreadPoolExecutor:
Packages¶
-
Free-threaded runtimes support packages from Artifact Repository, using either the shared PyPI repository or a customer-hosted repository. Packages from the Snowflake Anaconda channel aren’t available for free-threaded runtimes.
-
If a function or procedure specifies packages, the role that creates it and the roles that call it need access to the repository. Functions and procedures that don’t specify packages don’t need repository access. For the shared PyPI repository, this is the
SNOWFLAKE.PYPI_REPOSITORY_USERdatabase role, which is granted to PUBLIC by default. If your account administrator has revoked that grant, see Artifact Repository overview. For a customer-hosted repository, see Configure a customer-hosted artifact repository. -
Every package with a native (C) extension must declare that it supports free threading. Many packages don’t declare support yet. If a function imports an extension that doesn’t declare support, Snowflake doesn’t re-enable the GIL. Creating the function succeeds, but the function fails when it first runs and imports the extension. The error message is similar to the following:
-
To check whether a package publishes free-threaded wheels (for example,
cp314tfor3.14t), see the package’s documentation or the community compatibility tracker. -
Pure Python packages don’t need to declare support.
Shared state and thread safety¶
On a free-threaded runtime, workers and functions in the same query share one Python process, so they share
module-level state. This includes globals in your handler module and in the packages that you import. For example,
np.random.seed() and pd.set_option() change process-wide state that other workers see.
Follow these guidelines:
- Keep shared state limited, and load shared objects at module scope so that they are read-only after initialization.
- Keep mutable state local to each handler call.
- If you must share mutable state, protect it with a lock such as
threading.Lock. - Avoid frequent concurrent writes to the same shared objects. Python serializes these writes internally, which can make the code slower than on a standard runtime.
For common thread-safety pitfalls, see the community porting guide.
Limitations¶
The following features aren’t supported on free-threaded runtimes:
- The
lokyandmultiprocessingbackends ofjoblib. On free-threaded runtimes,joblibuses thethreadingbackend by default. You can also useconcurrent.futures.ThreadPoolExecutorfor parallel processing. - Emitting telemetry, such as log messages and trace events, from threads that you create in your handler.
- Python profiling of UDF and procedure handlers.
- Snowflake ML, including the
snowflake-ml-pythonpackage and models deployed to a warehouse from the Snowflake Model Registry.