5555import logging
5656import os
5757import urllib .request
58+ from asyncio .tasks import Task
5859from typing import Any , Dict , List , Optional , Union
5960
6061import aiohttp
62+ import nest_asyncio
6163import pkg_resources
6264import requests
6365import tqdm
6769
6870from .annotation import (
6971 BoxAnnotation ,
72+ CuboidAnnotation ,
73+ Point ,
7074 PolygonAnnotation ,
7175 Segment ,
7276 SegmentationAnnotation ,
73- Point ,
74- CuboidAnnotation ,
7577)
7678from .constants import (
7779 ANNOTATION_METADATA_SCHEMA_KEY ,
8183 DATASET_ID_KEY ,
8284 DATASET_ITEM_IDS_KEY ,
8385 DEFAULT_NETWORK_TIMEOUT_SEC ,
84- EMBEDDINGS_URL_KEY ,
8586 EMBEDDING_DIMENSION_KEY ,
87+ EMBEDDINGS_URL_KEY ,
8688 ERROR_ITEMS ,
8789 ERROR_PAYLOAD ,
8890 ERRORS_KEY ,
@@ -413,28 +415,33 @@ def populate_dataset(
413415
414416 agg_response = UploadResponse (json = {DATASET_ID_KEY : dataset_id })
415417
416- tqdm_local_batches = self .tqdm_bar (local_batches )
417-
418- tqdm_remote_batches = self .tqdm_bar (remote_batches )
419-
420418 async_responses : List [Any ] = []
421419
422- for batch in tqdm_local_batches :
423- payload = construct_append_payload (batch , update )
424- responses = self ._process_append_requests_local (
425- dataset_id , payload , update
420+ if local_batches :
421+ tqdm_local_batches = self .tqdm_bar (
422+ local_batches , desc = "Local file batches"
426423 )
427- async_responses .extend (responses )
428-
429- for batch in tqdm_remote_batches :
430- payload = construct_append_payload (batch , update )
431- responses = self ._process_append_requests (
432- dataset_id = dataset_id ,
433- payload = payload ,
434- update = update ,
435- batch_size = batch_size ,
424+
425+ for batch in tqdm_local_batches :
426+ payload = construct_append_payload (batch , update )
427+ responses = self ._process_append_requests_local (
428+ dataset_id , payload , update
429+ )
430+ async_responses .extend (responses )
431+
432+ if remote_batches :
433+ tqdm_remote_batches = self .tqdm_bar (
434+ remote_batches , desc = "Remote file batches"
436435 )
437- async_responses .extend (responses )
436+ for batch in tqdm_remote_batches :
437+ payload = construct_append_payload (batch , update )
438+ responses = self ._process_append_requests (
439+ dataset_id = dataset_id ,
440+ payload = payload ,
441+ update = update ,
442+ batch_size = batch_size ,
443+ )
444+ async_responses .extend (responses )
438445
439446 for response in async_responses :
440447 agg_response .update_response (response )
@@ -449,6 +456,8 @@ def _process_append_requests_local(
449456 local_batch_size : int = 10 ,
450457 ):
451458 def get_files (batch ):
459+ for item in batch :
460+ item [UPDATE_KEY ] = update
452461 request_payload = [
453462 (
454463 ITEMS_KEY ,
@@ -481,14 +490,20 @@ def get_files(batch):
481490 files_per_request .append (get_files (batch ))
482491 payload_items .append (batch )
483492
484- loop = asyncio .get_event_loop ()
485- responses = loop .run_until_complete (
486- self .make_many_files_requests_asynchronously (
487- files_per_request ,
488- f"dataset/{ dataset_id } /append" ,
489- )
493+ future = self .make_many_files_requests_asynchronously (
494+ files_per_request ,
495+ f"dataset/{ dataset_id } /append" ,
490496 )
491497
498+ try :
499+ loop = asyncio .get_event_loop ()
500+ except RuntimeError : # no event loop running:
501+ loop = asyncio .new_event_loop ()
502+ responses = loop .run_until_complete (future )
503+ else :
504+ nest_asyncio .apply (loop )
505+ return loop .run_until_complete (future )
506+
492507 def close_files (request_items ):
493508 for item in request_items :
494509 # file buffer in location [1][1]
0 commit comments