Skip to content
This repository was archived by the owner on Jun 7, 2021. It is now read-only.

Commit 5c3ead9

Browse files
author
Joel Collins
committed
Added comments to example
1 parent 08c0fd4 commit 5c3ead9

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

example/mjpeg-stream.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727

2828

2929
class StreamGenerator:
30+
"""An example image streamer class"""
3031
def __init__(self):
31-
self.stream = io.BytesIO()
32-
self.running = False
33-
self.event = asyncio.Event()
32+
self.stream = io.BytesIO() # Byte stream to hold the latest JPEG frame
33+
self.running = False # Is the stream frame generator running
34+
self.event = asyncio.Event() # Event to signal a new frame is ready
3435

3536
def _start_runner(self):
3637
print("Starting frame runner")
38+
# Run frame generator loop in a coroutine task
3739
task = asyncio.create_task(self.frame_loop())
3840
self.running = True
3941
return task
@@ -48,18 +50,22 @@ def generate_new_dummy_image(self):
4850
"Current time: {}".format(datetime.now().strftime("%d/%m/%Y, %H:%M:%S")),
4951
)
5052

53+
# Save new image to the stream
5154
image.save(self.stream, format="JPEG")
5255

5356
async def frame_loop(self):
5457
while True:
55-
await asyncio.sleep(1) # Only serve frames at 1fps
58+
# Only serve frames at 1fps
59+
await asyncio.sleep(1)
60+
# Signal we're in the middle of writing a new frame
5661
self.event.clear()
5762
# Reset stream
5863
self.stream.seek(0)
5964
self.stream.truncate()
6065

6166
# Generate new dumm image
6267
self.generate_new_dummy_image()
68+
# Signal a new frame is ready
6369
self.event.set()
6470

6571
async def stream_generator(self):

0 commit comments

Comments
 (0)