Python Forum
RuntimeError: generator raised StopIteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RuntimeError: generator raised StopIteration
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help RuntimeError: no running event loop marpaslight 5 3,726 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  bleak library RuntimeError: This event loop is already running alice93 3 4,093 Sep-30-2021, 08:06 AM
Last Post: alice93
  RuntimeError: This event loop is already running newbie2019 2 6,953 Sep-30-2020, 06:59 PM
Last Post: forest44
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 6,148 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  StopIteration exception when mock PostgreSQL connection in several tests igor87z 1 2,919 Jun-10-2020, 06:16 PM
Last Post: ibreeden
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 5,923 Nov-08-2019, 05:00 PM
Last Post: Gribouillis
  RuntimeError: can't open file Hadad 2 5,349 Aug-27-2019, 04:23 PM
Last Post: Hadad
  multi-line messages in raised exceptions? Skaperen 3 7,331 Aug-01-2019, 02:17 AM
Last Post: Skaperen
  ImportError has not raised in pkg_resources module but expected tamilselvanvjm 2 2,519 Jun-19-2019, 10:06 AM
Last Post: tamilselvanvjm
  RuntimeError: dictionary changed size during iteration Shreeniket987 3 3,737 Jun-01-2019, 01:22 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020