Quellcode durchsuchen

Fixing memory usage when writting animations

The options -b and -B specify a buffer size for images waiting to we writtent
on disk
Yann Weber vor 5 Jahren
Ursprung
Commit
ab7876e8d9
1 geänderte Dateien mit 25 neuen und 6 gelöschten Zeilen
  1. 25
    6
      gte/__main__.py

+ 25
- 6
gte/__main__.py Datei anzeigen

@@ -113,6 +113,14 @@ parser_gen.add_argument('--no-diagonals', '-d', action='store_const',
113 113
 parser_gen.add_argument('--threads', '-t', type=int, metavar='N',
114 114
                         default=os.cpu_count()-1,
115 115
                         help="Animation writting threads")
116
+parser_gen.add_argument('--buffer-min', '-b', type=int, metavar='N',
117
+                        default=10,
118
+                        help="Start populating animation buffer when this \
119
+amount of images are in the buffer")
120
+parser_gen.add_argument('--buffer-max', '-B', type=int, metavar='N',
121
+                        default=100,
122
+                        help="Start a buffer flush when this amount of images \
123
+are in the buffer")
116 124
 
117 125
 args = parser.parse_args()
118 126
 
@@ -245,10 +253,12 @@ elif 'output_dir' in args:
245 253
             im.save(fname)
246 254
             logger.debug('Image %s saved in %.3fs' % (fname, 
247 255
                                                       time.time() - st1))
256
+            del(im)
248 257
 
249 258
         write_pool = Pool(args.threads)
250 259
         async_res = []
251 260
         img_count = 0
261
+        img_total = args.steps if args.anim_div <=1 else args.steps / (args.anim_div - 1)
252 262
     w = World(args.world_height, args.world_width, gray=args.gray)
253 263
     prog = rpnlib.RpnExpr.from_string(args.prog)
254 264
     turmits = [LivingTurmit(world=w, prog=prog, diag=not args.no_diagonals)
@@ -258,7 +268,7 @@ elif 'output_dir' in args:
258 268
     start = time.time()
259 269
     for step in range(args.steps):
260 270
         if step % 512 == 1:
261
-            msg = 'Step %d/%d %dus/step fractdim=%.3f'
271
+            msg = 'Step %d/%d avg: %dus/step fractdim=%.3f'
262 272
             msg %= (step, args.steps,
263 273
                     ((time.time() - start)*1000000)//step//len(turmits),
264 274
                     w.fractdim())
@@ -273,14 +283,23 @@ elif 'output_dir' in args:
273 283
             fname = os.path.join(args.output_dir, fname)
274 284
             im = Image.fromarray(np.uint8(w._val))
275 285
             async_res.append(write_pool.apply_async(save_pool, (im, fname)))
276
-            while async_res[0].ready():
277
-                async_res[0].get()
278
-                async_res.pop(0)
286
+            if len(async_res) > args.buffer_max:
287
+                logger.debug('Flushing animation on disk to empty RAM')
288
+                while len(async_res) > args.buffer_min:
289
+                    while async_res[0].ready():
290
+                        async_res[0].get()
291
+                        async_res.pop(0)
292
+                msg = '%d/%d images written at avg %.0f img/s ETL : %ds'
293
+                img_prog = img_count - args.buffer_min
294
+                img_pers = img_prog/((time.time() - start))
295
+                etl = (img_total - img_prog) // img_pers
296
+                msg %= (img_prog, img_total, img_pers, etl)
297
+                logger.info(msg)
279 298
     
280 299
     if args.output_dir is not None:
281
-        while async_res[0].ready():
300
+        while len(async_res) and async_res[0].ready():
282 301
             async_res[0].get()
283
-            sync_res.pop(0)
302
+            async_res.pop(0)
284 303
         write_pool.close()
285 304
         logger.info('Waiting for animation to be written on disk')
286 305
         img_st1 = None

Laden…
Abbrechen
Speichern