Skip to main content

freethreading — Thread-first true parallelism

PyPI Version Python Versions License CI Build Codecov Docs

freethreading is a lightweight wrapper that provides a unified API for true parallel execution in Python. It automatically uses threading on free-threaded Python builds (where the Global Interpreter Lock (GIL) is disabled) and falls back to multiprocessing on standard ones. This enables true parallelism across Python versions while preferring the efficiency of threads over processes whenever possible.

Installation

To install freethreading, simply run:

pip install free-threading

To install the latest development version, you can run:

pip install git+https://github.com/iskandergaba/free-threading.git

Quick Start

freethreading is a drop-in replacement for most pre-existing threading and multiprocessing code. To achieve this, the module exposes only non-deprecated common functionality shared between both backends while discarding any backend-specific APIs. The following examples show how to get started.

freethreading remains consistent with the standard library, so wrapper classes work as drop-in replacements for those used by threading and multiprocessing. Here's how they work:

# threading
from queue import Queue
from threading import Event, Lock

# multiprocessing
from multiprocessing import Event, Lock, Queue

# freethreading (replaces both)
from freethreading import Event, Lock, Queue

if __name__ == "__main__":
    event = Event()
    lock = Lock()
    queue = Queue()
    print(lock.acquire())
    event.set()
    queue.put("data")
    print(event.is_set())
    print(queue.get())
    lock.release()

Output:

True
True
data

freethreading functions merge as much functionality from both backends as possible to ensure consistent behavior across backends and simplify adoption. Here's what that looks like:

# threading
from threading import enumerate, get_ident

# multiprocessing
from multiprocessing import active_children
from os import getpid

# freethreading (replaces both)
from freethreading import active_children, enumerate, get_ident

if __name__ == "__main__":
    print(len(active_children()))  # excludes current thread or process
    print(len(enumerate()))  # includes current thread or process
    print(get_ident())  # current thread or process identifier

Output:

0
1
140247834...

Only Worker, WorkerPool, WorkerPoolExecutor, and current_worker differ from the standard library naming, using "worker" as a term for both threads and processes. Below is an example:

# threading
from concurrent.futures import ThreadPoolExecutor
from threading import Thread, current_thread

# multiprocessing
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import Process, current_process

# freethreading (replaces both)
from freethreading import Worker, WorkerPool, WorkerPoolExecutor, current_worker


def greet():
    print(f"Hello from {current_worker().name}!")


def square(x):
    return x * x


if __name__ == "__main__":
    # MainThread or MainProcess
    print(current_worker().name)

    # Using Worker (Thread or Process) to run a task
    w = Worker(target=greet, name="MyWorker")
    w.start()
    w.join()

    # Using WorkerPool (Pool or ThreadPool) to distribute work
    with WorkerPool(workers=2) as pool:
        print(pool.map(square, range(5)))

    # Using WorkerPoolExecutor (ThreadPoolExecutor or ProcessPoolExecutor) to run a task
    with WorkerPoolExecutor(max_workers=2) as executor:
        future = executor.submit(greet)

Output (Standard Python):

MainProcess
Hello from MyWorker!
[0, 1, 4, 9, 16]
Hello from ForkServerProcess-4!

Output (Free-threaded Python):

MainThread
Hello from MyWorker!
[0, 1, 4, 9, 16]
Hello from ThreadPoolExecutor-0_0!

Documentation

For more details, check out the full documentation at freethreading.readthedocs.io.

Metadata

Release files for free-threading 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for free-threading 1.2.0
File Size Uploaded
free_threading-1.2.0.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for free-threading 1.2.0
File Interpreter ABI Platform
free_threading-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.9 kB

Release files / free_threading-1.2.0.tar.gz

Download URL free_threading-1.2.0.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d43b7cd93397bc4dbfe6689e32a6d5cce601ba2d93818df9e70e7dfcdc7ee668
BLAKE2b-256 checksum
How to use checksums
47e7b48101e7bb9db993493a1e5433224d7a5e5a2282b075083c461417f1ed13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / free_threading-1.2.0-py3-none-any.whl

Download URL free_threading-1.2.0-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
921d64ed74e6e822008d066d66a3e8eb6231ce2392a1e86d4c6eba5d3da16be8
BLAKE2b-256 checksum
How to use checksums
66c9df06cd68e563e9ee7c3f5889a36ee2fd27480d559ebf10f5244eed180c4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page