|
55 | 55 | import logging |
56 | 56 | import os |
57 | 57 | import urllib.request |
| 58 | +from asyncio.tasks import Task |
58 | 59 | from typing import Any, Dict, List, Optional, Union |
59 | 60 |
|
60 | 61 | import aiohttp |
| 62 | +import nest_asyncio |
61 | 63 | import pkg_resources |
62 | 64 | import requests |
63 | 65 | import tqdm |
|
67 | 69 |
|
68 | 70 | from .annotation import ( |
69 | 71 | BoxAnnotation, |
| 72 | + CuboidAnnotation, |
| 73 | + Point, |
70 | 74 | PolygonAnnotation, |
71 | 75 | Segment, |
72 | 76 | SegmentationAnnotation, |
73 | | - Point, |
74 | | - CuboidAnnotation, |
75 | 77 | ) |
76 | 78 | from .constants import ( |
77 | 79 | ANNOTATION_METADATA_SCHEMA_KEY, |
|
81 | 83 | DATASET_ID_KEY, |
82 | 84 | DATASET_ITEM_IDS_KEY, |
83 | 85 | DEFAULT_NETWORK_TIMEOUT_SEC, |
84 | | - EMBEDDINGS_URL_KEY, |
85 | 86 | EMBEDDING_DIMENSION_KEY, |
| 87 | + EMBEDDINGS_URL_KEY, |
86 | 88 | ERROR_ITEMS, |
87 | 89 | ERROR_PAYLOAD, |
88 | 90 | ERRORS_KEY, |
@@ -462,14 +464,20 @@ def get_files(batch): |
462 | 464 | files_per_request.append(get_files(batch)) |
463 | 465 | payload_items.append(batch) |
464 | 466 |
|
465 | | - loop = asyncio.get_event_loop() |
466 | | - responses = loop.run_until_complete( |
467 | | - self.make_many_files_requests_asynchronously( |
468 | | - files_per_request, |
469 | | - f"dataset/{dataset_id}/append", |
470 | | - ) |
| 467 | + future = self.make_many_files_requests_asynchronously( |
| 468 | + files_per_request, |
| 469 | + f"dataset/{dataset_id}/append", |
471 | 470 | ) |
472 | 471 |
|
| 472 | + try: |
| 473 | + loop = asyncio.get_running_loop() |
| 474 | + except RuntimeError: # no event loop running: |
| 475 | + loop = asyncio.new_event_loop() |
| 476 | + responses = loop.run_until_complete(future) |
| 477 | + else: |
| 478 | + nest_asyncio.apply(loop) |
| 479 | + return asyncio.run(loop.create_task(future)) |
| 480 | + |
473 | 481 | def close_files(request_items): |
474 | 482 | for item in request_items: |
475 | 483 | # file buffer in location [1][1] |
|
0 commit comments