(loader, queue)
| 214 | |
| 215 | |
| 216 | async def dispatch_queue_batch(loader, queue): |
| 217 | # Collect all keys to be loaded in this dispatch |
| 218 | keys = [loaded.key for loaded in queue] |
| 219 | |
| 220 | # Call the provided batch_load_fn for this loader with the loader queue's keys. |
| 221 | batch_future = loader.batch_load_fn(keys) |
| 222 | |
| 223 | # Assert the expected response from batch_load_fn |
| 224 | if not batch_future or not iscoroutine(batch_future): |
| 225 | return failed_dispatch( # pragma: no cover |
| 226 | loader, |
| 227 | queue, |
| 228 | TypeError( |
| 229 | ( |
| 230 | "DataLoader must be constructed with a function which accepts " |
| 231 | "Iterable<key> and returns Future<Iterable<value>>, but the function did " |
| 232 | "not return a Coroutine: {}." |
| 233 | ).format(batch_future) |
| 234 | ), |
| 235 | ) |
| 236 | |
| 237 | try: |
| 238 | values = await batch_future |
| 239 | if not isinstance(values, Iterable): |
| 240 | raise TypeError( # pragma: no cover |
| 241 | ( |
| 242 | "DataLoader must be constructed with a function which accepts " |
| 243 | "Iterable<key> and returns Future<Iterable<value>>, but the function did " |
| 244 | "not return a Future of a Iterable: {}." |
| 245 | ).format(values) |
| 246 | ) |
| 247 | |
| 248 | values = list(values) |
| 249 | if len(values) != len(keys): |
| 250 | raise TypeError( # pragma: no cover |
| 251 | ( |
| 252 | "DataLoader must be constructed with a function which accepts " |
| 253 | "Iterable<key> and returns Future<Iterable<value>>, but the function did " |
| 254 | "not return a Future of a Iterable with the same length as the Iterable " |
| 255 | "of keys." |
| 256 | "\n\nKeys:\n{}" |
| 257 | "\n\nValues:\n{}" |
| 258 | ).format(keys, values) |
| 259 | ) |
| 260 | |
| 261 | # Step through the values, resolving or rejecting each Future in the |
| 262 | # loaded queue. |
| 263 | for loaded, value in zip(queue, values): |
| 264 | if isinstance(value, Exception): |
| 265 | loaded.future.set_exception(value) |
| 266 | else: |
| 267 | loaded.future.set_result(value) |
| 268 | |
| 269 | except Exception as e: |
| 270 | return failed_dispatch(loader, queue, e) |
| 271 | |
| 272 | |
| 273 | def failed_dispatch(loader, queue, error): |
no test coverage detected