mailwal

๐Ÿ๐Ÿš€ [3.15] thread-safe iterators

Rodrigo | Mathspp

October 29, 2026

๐Ÿ๐Ÿš€ [3.15] thread-safe iterators

day 13 of 15 days of Python 3.15

Hey there, ๐Ÿ‘‹

How is your Python ๐Ÿ going?

In this email weโ€™ll talk about writing and using thread-safe iterators. ๐Ÿš€

Python 3.15 logo. Purple heart in the background with two symmetrical snakes, yellow and blue, in front. Some release highlights frame the whole logo. Logo by Marie Nordin and Georgi Ker.

See the future schedule here and links to the previous days: https://mathspp.com/python-315

Iterators are not thread-safe

Iterators are not thread-safe out of the box.

For example, generators will raise ValueError if you try to access them from one thread while another thread is running the generator already.

Python 3.15 provides the tools to work with this.

Serialising an iterator

Given an iterator, you can now create a thread-safe version of it with

import threading

it = ...  # Non-thread-safe iterator
threadsafe_it = threading.serialize_iterator(it)

Serialising all iterators from an iterator factory

If you have an โ€œiterator factoryโ€, like something from itertools or a generator, you can use @threading.synchronize_iterator to automatically serialise all the iterators produce.

E.g., hereโ€™s how to write a thread-safe generator:

import threading

@threading.synchronize_iterator
def my_threadsafe_generator():
    ...

The generator function my_threadsafe_generator will automatically produce thread-safe iterators.

Further reading

You can learn more about thread-safe iterators and the new 3.15 tools here: https://mathspp.com/blog/threadsafe-iterators

Tomorrow youโ€™ll learn about the new โ€œabi3tโ€, namely what it means and the silent impact itโ€™ll have on you and the community!

See the future schedule here and links to the previous days: https://mathspp.com/python-315

tw
yt
in

You received this email (at [subscriber]) because you got my free Python book โ€œPydonโ€™tsโ€ or one of my other books/cheatsheets (or because you signed up directly!).

Rodrigo ๐Ÿ๐Ÿš€

ยฉ 2026 mathspp insider ๐Ÿ๐Ÿš€

228 Park Ave S, #29976, New York, New York 10003, United States