@@ -25,6 +25,12 @@ def add_arguments(self, parser):
2525 default = "" ,
2626 help = "Run house-keeping tasks." ,
2727 )
28+ parser .add_argument (
29+ "--no-parallel" ,
30+ action = "store_true" ,
31+ dest = "no_parallel" ,
32+ help = "Process images sequentially without parallelization." ,
33+ )
2834 parser .add_argument (
2935 "field" ,
3036 nargs = "*" ,
@@ -87,18 +93,16 @@ def _process_field(self, field, options):
8793 force = options .get ("force" ),
8894 )
8995
90- pool = mp .Pool ()
91-
9296 fn = partial (
9397 _process_instance ,
9498 field = field ,
9599 housekeep = options .get ("housekeep" ),
96100 force = options .get ("force" ),
97101 )
98102
99- try :
103+ if options . get ( "no_parallel" ) :
100104 for index , (instance , errors ) in enumerate (
101- pool . imap (fn , queryset .iterator (chunk_size = 100 ))
105+ map (fn , queryset .iterator (chunk_size = 100 ))
102106 ):
103107 if errors :
104108 self .stderr .write ("\n " .join (errors ))
@@ -112,10 +116,28 @@ def _process_field(self, field, options):
112116 # if not done already
113117 instance ._skip_generate_files = True
114118 instance .save ()
115-
116- finally :
117- pool .close ()
118- pool .join ()
119+ else :
120+ pool = mp .Pool ()
121+ try :
122+ for index , (instance , errors ) in enumerate (
123+ pool .imap (fn , queryset .iterator (chunk_size = 100 ))
124+ ):
125+ if errors :
126+ self .stderr .write ("\n " .join (errors ))
127+
128+ progress = "*" * (50 * index // count )
129+ self .stdout .write (
130+ f"\r |{ progress .ljust (50 )} | { index + 1 } /{ count } " , ending = ""
131+ )
132+
133+ # Save instance once for good measure; fills in width/height
134+ # if not done already
135+ instance ._skip_generate_files = True
136+ instance .save ()
137+
138+ finally :
139+ pool .close ()
140+ pool .join ()
119141
120142 self .stdout .write ("\r |{}| {}/{}" .format ("*" * 50 , count , count ))
121143
0 commit comments