99import re
1010import time
1111import traceback
12+ import typing
1213from dataclasses import dataclass
1314from datetime import datetime
1415from itertools import islice
5758URI_THRESHOLD = 4 * 1024 - 120
5859
5960
61+ def run_async (f ):
62+ from threading import Thread
63+
64+ response = [None ]
65+
66+ def foo (f : typing .Callable , res ):
67+ res [0 ] = asyncio .run (f ) # noqa
68+
69+ thread = Thread (target = foo , args = (f , response ))
70+ thread .start ()
71+ thread .join ()
72+ return response [0 ]
73+
74+
6075@dataclass
6176class Report :
6277 failed_annotations : list
@@ -390,7 +405,7 @@ def execute(self):
390405 len (items_to_upload ), description = "Uploading Annotations"
391406 )
392407 try :
393- asyncio . run (self .run_workers (items_to_upload ))
408+ run_async (self .run_workers (items_to_upload ))
394409 except Exception :
395410 logger .debug (traceback .format_exc ())
396411 self ._response .errors = AppException ("Can't upload annotations." )
@@ -735,7 +750,7 @@ def execute(self):
735750 except KeyError :
736751 missing_annotations .append (name )
737752 try :
738- asyncio . run (self .run_workers (items_to_upload ))
753+ run_async (self .run_workers (items_to_upload ))
739754 except Exception as e :
740755 logger .debug (e )
741756 self ._response .errors = AppException ("Can't upload annotations." )
@@ -933,7 +948,7 @@ def execute(self):
933948 size = annotation_file .tell ()
934949 annotation_file .seek (0 )
935950 if size > BIG_FILE_THRESHOLD :
936- uploaded = asyncio . run (
951+ uploaded = run_async (
937952 self ._service_provider .annotations .upload_big_annotation (
938953 project = self ._project ,
939954 folder = self ._folder ,
@@ -945,7 +960,7 @@ def execute(self):
945960 if not uploaded :
946961 self ._response .errors = constants .INVALID_JSON_MESSAGE
947962 else :
948- response = asyncio . run (
963+ response = run_async (
949964 self ._service_provider .annotations .upload_small_annotations (
950965 project = self ._project ,
951966 folder = self ._folder ,
@@ -1518,14 +1533,15 @@ def execute(self):
15181533 sort_response = self ._service_provider .annotations .get_upload_chunks (
15191534 project = self ._project ,
15201535 item_ids = list (id_item_map ),
1536+
15211537 )
15221538 large_item_ids = set (map (itemgetter ("id" ), sort_response ["large" ]))
15231539 large_items : List [BaseItemEntity ] = list (
15241540 filter (lambda item : item .id in large_item_ids , items )
15251541 )
15261542 small_items : List [List [dict ]] = sort_response ["small" ]
15271543 try :
1528- annotations = asyncio . run (self .run_workers (large_items , small_items ))
1544+ annotations = run_async (self .run_workers (large_items , small_items ))
15291545 except Exception as e :
15301546 logger .error (e )
15311547 self ._response .errors = AppException ("Can't get annotations." )
@@ -1731,7 +1747,7 @@ def execute(self):
17311747 )
17321748 small_items : List [List [dict ]] = sort_response ["small" ]
17331749 try :
1734- asyncio . run (
1750+ run_async (
17351751 self .run_workers (
17361752 large_items , small_items , folder , new_export_path
17371753 )
0 commit comments