Назад к задачамПолучайте помощь с лайвкодингом в реальном времени с Sobes Copilot
Junior — Senior
4
Thread‑safe iterator implementation
Условие задачи
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