import asyncio
from crewai import Crew, Agent, Task
# 코드 실행이 허용된 에이전트 생성
coding_agent = Agent(
role="Python Data Analyst",
goal="Analyze data and provide insights using Python",
backstory="You are an experienced data analyst with strong Python skills.",
allow_code_execution=True
)
# 코드 실행이 필요한 태스크 생성
task_1 = Task(
description="Analyze the first dataset and calculate the average age of participants. Ages: {ages}",
agent=coding_agent
)
task_2 = Task(
description="Analyze the second dataset and calculate the average age of participants. Ages: {ages}",
agent=coding_agent
)
# 두 개의 crew를 생성하고 태스크 추가
crew_1 = Crew(agents=[coding_agent], tasks=[task_1])
crew_2 = Crew(agents=[coding_agent], tasks=[task_2])
# 여러 crew를 비동기적으로 실행하고 모든 작업이 완료될 때까지 기다리는 비동기 함수
async def async_multiple_crews():
result_1 = crew_1.kickoff_async(inputs={"ages": [25, 30, 35, 40, 45]})
result_2 = crew_2.kickoff_async(inputs={"ages": [20, 22, 24, 28, 30]})
# 두 crew가 모두 완료될 때까지 기다림
results = await asyncio.gather(result_1, result_2)
for i, result in enumerate(results, 1):
print(f"Crew {i} Result:", result)
# 비동기 함수 실행
asyncio.run(async_multiple_crews())