Get population information for multiple cities. Args: cities: A list of city names. Returns: A dictionary with population data for each city.
(cities: List[str], tool_context: ToolContext)
| 172 | |
| 173 | |
| 174 | async def get_population(cities: List[str], tool_context: ToolContext) -> dict: |
| 175 | """Get population information for multiple cities. |
| 176 | |
| 177 | Args: |
| 178 | cities: A list of city names. |
| 179 | |
| 180 | Returns: |
| 181 | A dictionary with population data for each city. |
| 182 | """ |
| 183 | # Simulate async processing time proportional to number of cities (non-blocking) |
| 184 | await asyncio.sleep(len(cities) * 0.5) |
| 185 | |
| 186 | # Mock population data |
| 187 | populations = { |
| 188 | 'New York': 8336817, |
| 189 | 'London': 9648110, |
| 190 | 'Tokyo': 13960000, |
| 191 | 'San Francisco': 873965, |
| 192 | 'Paris': 2161000, |
| 193 | 'Sydney': 5312163, |
| 194 | } |
| 195 | |
| 196 | results = {} |
| 197 | for city in cities: |
| 198 | results[city] = populations.get(city, 1000000) # default 1M if not found |
| 199 | |
| 200 | # Store in context for testing thread safety |
| 201 | if 'population_requests' not in tool_context.state: |
| 202 | tool_context.state['population_requests'] = [] |
| 203 | tool_context.state['population_requests'].append( |
| 204 | {'cities': cities, 'results': results} |
| 205 | ) |
| 206 | |
| 207 | return { |
| 208 | 'populations': results, |
| 209 | 'total_population': sum(results.values()), |
| 210 | 'cities_count': len(cities), |
| 211 | } |
| 212 | |
| 213 | |
| 214 | root_agent = Agent( |