fix: address lint issues in streaming implementation

- Remove whitespace from blank lines
- Refactor try-except out of loop for better performance
- Use list() instead of append in loop for better performance

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-10-20 12:24:36 +00:00
parent b664637afa
commit 0af7e04cde
2 changed files with 8 additions and 16 deletions

View File

@@ -792,7 +792,7 @@ class Crew(FlowTrackable, BaseModel):
def event_generator():
for event in crew.kickoff_stream(inputs={"topic": "AI"}):
yield f"data: {json.dumps(event)}\\n\\n"
return StreamingResponse(
event_generator(),
media_type="text/event-stream"
@@ -868,15 +868,13 @@ class Crew(FlowTrackable, BaseModel):
try:
while not completion_event.is_set() or not event_queue.empty():
try:
event = event_queue.get(timeout=0.1)
event = event_queue.get(timeout=0.1) if not event_queue.empty() else None
if event is not None:
yield event
except queue.Empty:
continue
if exception_holder["exception"]:
raise exception_holder["exception"]
finally:
thread.join(timeout=1)

View File

@@ -4757,9 +4757,7 @@ def test_crew_kickoff_stream(researcher):
crew = Crew(agents=[researcher], tasks=[task])
events = []
for event in crew.kickoff_stream():
events.append(event)
events = list(crew.kickoff_stream())
assert len(events) > 0
@@ -4784,9 +4782,7 @@ def test_crew_kickoff_stream_with_inputs(researcher):
crew = Crew(agents=[researcher], tasks=[task])
events = []
for event in crew.kickoff_stream(inputs={"topic": "machine learning"}):
events.append(event)
events = list(crew.kickoff_stream(inputs={"topic": "machine learning"}))
assert len(events) > 0
@@ -4806,9 +4802,7 @@ def test_crew_kickoff_stream_includes_llm_chunks(researcher):
crew = Crew(agents=[researcher], tasks=[task])
events = []
for event in crew.kickoff_stream():
events.append(event)
events = list(crew.kickoff_stream())
event_types = [event["type"] for event in events]