Back to tasksGet help with live coding in real time with Sobes Copilot
Junior — Senior
50
Thread‑safe iterator implementation
Task condition
Implement a class that conforms to Python's iterator protocol and guarantees safe operation when used simultaneously from multiple threads. The iterator must correctly handle concurrent accesses, preserving the integrity of the internal state during iteration.
import threading
class Iterable:
def __init__(self, data):
self.data = data
self.cursor = 0
# TODO: добавить потокобезопасность
def __iter__(self):
return self
def __next__(self):
# TODO: потокобезопасный доступ к self.cursor и self.data
pass