Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Lib/test/test_free_threading/test_itertools_batched.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
import sys
from threading import Thread, Barrier
from itertools import batched
from test.support import threading_helper


class EnumerateThreading(unittest.TestCase):

@threading_helper.reap_threads
@threading_helper.requires_working_threading()
def test_threading(self):
number_of_threads = 10
number_of_iterations = 20
barrier = Barrier(number_of_threads)
def work(it):
barrier.wait()
while True:
try:
_ = next(it)
except StopIteration:
break

data = tuple(range(1000))
for it in range(number_of_iterations):
batch_iterator = batched(data, 2)
worker_threads = []
for ii in range(number_of_threads):
worker_threads.append(
Thread(target=work, args=[batch_iterator]))
for t in worker_threads:
t.start()
for t in worker_threads:
t.join()

barrier.reset()

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make concurrent iterations over :class:`itertools.batched` safe under free-threading.
20 changes: 19 additions & 1 deletion Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,20 @@ static PyObject *
batched_next(batchedobject *bo)
{
Py_ssize_t i;
Py_ssize_t n = bo->batch_size;
Py_ssize_t n = FT_ATOMIC_LOAD_SSIZE_RELAXED(bo->batch_size);
PyObject *it = bo->it;
PyObject *item;
PyObject *result;

#ifdef Py_GIL_DISABLED
if (it == NULL) {
return NULL;
}
#else
if (n < 0) {
return NULL;
}
#endif
result = PyTuple_New(n);
if (result == NULL) {
return NULL;
Expand All @@ -213,19 +219,31 @@ batched_next(batchedobject *bo)
if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
/* Input raised an exception other than StopIteration */
#ifdef Py_GIL_DISABLED
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
#else
Py_CLEAR(bo->it);
#endif
Py_DECREF(result);
return NULL;
}
PyErr_Clear();
}
if (i == 0) {
#ifdef Py_GIL_DISABLED
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
#else
Py_CLEAR(bo->it);
#endif
Py_DECREF(result);
return NULL;
}
if (bo->strict) {
#ifdef Py_GIL_DISABLED
FT_ATOMIC_STORE_SSIZE_RELAXED(bo->batch_size, -1);
#else
Py_CLEAR(bo->it);
#endif
Py_DECREF(result);
PyErr_SetString(PyExc_ValueError, "batched(): incomplete batch");
return NULL;
Expand Down
Loading