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 ,
@@ -413,28 +415,33 @@ def populate_dataset(
413
415
414
416
agg_response = UploadResponse (json = {DATASET_ID_KEY : dataset_id })
415
417
416
- tqdm_local_batches = self .tqdm_bar (local_batches )
417
-
418
- tqdm_remote_batches = self .tqdm_bar (remote_batches )
419
-
420
418
async_responses : List [Any ] = []
421
419
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"
426
423
)
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"
436
435
)
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 )
438
445
439
446
for response in async_responses :
440
447
agg_response .update_response (response )
@@ -449,6 +456,8 @@ def _process_append_requests_local(
449
456
local_batch_size : int = 10 ,
450
457
):
451
458
def get_files (batch ):
459
+ for item in batch :
460
+ item [UPDATE_KEY ] = update
452
461
request_payload = [
453
462
(
454
463
ITEMS_KEY ,
@@ -481,14 +490,20 @@ def get_files(batch):
481
490
files_per_request .append (get_files (batch ))
482
491
payload_items .append (batch )
483
492
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" ,
490
496
)
491
497
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
+
492
507
def close_files (request_items ):
493
508
for item in request_items :
494
509
# file buffer in location [1][1]
0 commit comments