Python Forum
RuntimeError: generator raised StopIteration - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: RuntimeError: generator raised StopIteration (/thread-33093.html)



RuntimeError: generator raised StopIteration - quest - Mar-28-2021

Hello,
I created this class:

class ssource:
    def __init__(self, n):
        self.list1=[]
        self.list2=[]
        self.i=0
        self.max=n
        self.At=stream(self, self.list1)
        self.Bt=stream(self, self.list2)
        
    def __next__(self):
        if self.i>=self.max: raise StopIteration
        print(f"NEXTSYNC:{self.i}")
        self.list1.append(f'A{self.i}')
        self.list2.append(f'B{self.i}')
        self.i += 1
And I am testing my class with this code script:
src=ssource(10)
Ai=src.Ait
Bi=src.Bit

for i in range(5):
    print(f'A->{next(Ai)}')
for i in range(8):
    print(f'B->{next(Bi)}')
for a in Ai : print(f'A->{a}')
And I am getting this error:

Error:
NEXTSYNC:0 A->A0 NEXTSYNC:1 A->A1 NEXTSYNC:2 A->A2 NEXTSYNC:3 A->A3 NEXTSYNC:4 A->A4 B->B0 B->B1 B->B2 B->B3 B->B4 NEXTSYNC:5 B->B5 NEXTSYNC:6 B->B6 NEXTSYNC:7 B->B7 A->A5 A->A6 A->A7 NEXTSYNC:8 A->A8 NEXTSYNC:9 A->A9 --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-36-b322da59c757> in stream(src, fifo) 4 while True: ----> 5 if len(fifo)==0: next(src) 6 yield(fifo.pop(0)) <ipython-input-36-b322da59c757> in __next__(self) 17 def __next__(self): ---> 18 if self.i>=self.max: raise StopIteration 19 print(f"NEXTSYNC:{self.i}") StopIteration: The above exception was the direct cause of the following exception: RuntimeError Traceback (most recent call last) <ipython-input-39-9b42e44489d5> in <module> 7 for i in range(8): 8 print(f'B->{next(Bi)}') ----> 9 for a in Ai : print(f'A->{a}') RuntimeError: generator raised StopIteration
here is my another function
def stream(src, flist):
    try:
        while True:
            while len(flist)==0: next(src) 
            yield(flist.pop(0))
    except StopIteration:
        pass
#        raise StopIteration



RE: RuntimeError: generator raised StopIteration - quest - Mar-28-2021

Hello again,

I did nothing but now it is ok. I am studying on jupyter notebook maybe because of some connection problems I got this error.

OR If you have any idea, you are very welcome but it is not ok and I do not know how it is ok