Home > other >  Multithreaded cv2. Resize memory leaks, ask for advice
Multithreaded cv2. Resize memory leaks, ask for advice

Time:03-10

Phenomenon:
Loop over 1 w, discover python process memory continue to rise, very high res. Code did not see the problem, seek advice
Each loop, ThreadPoolExecutor is a new object

The code is as follows:

 

The import requests
The import cv2
The import time
The import OS
The import sys
The from concurrent. Futures import ThreadPoolExecutor, wait
The from datetime import datetime

Timeout_second=1
Tmp_jpg="tmp_jpg"
If not OS. Path. The exists (tmp_jpg) :
OS. The mkdir (tmp_jpg)

The req=requests. Get (" https://img14.360buyimg.com/babel/s1180x940_jfs/t1/169636/37/9236/297058/6040a6beEce62815f/d5a845163b127dd5.jpg ")
Tmp_jpg file_path="./"+ +"/target_file "
Open (file_path, 'wb). Write (the req. Content)

Def get_pic_by_ur () :
Try:
Item_tmp_img=file_path
Img=cv2. Imread (item_tmp_img)
Cv2. Resize (img, (300, 300))
Except the Exception as e:
Print (" error, need_exist ")
Sys. Exit (1)
Finally:
Pass


C=0
Thread_count=20
If len (sys. Argv)==2:
Thread_count=int (sys. Argv [1])
Print (" thread_count="+ STR (thread_count))
Time. Sleep (3)
While True:
Executor=ThreadPoolExecutor (thread_count, thread_name_prefix="process_image_")
Result_list=[]
For I in range (thread_count) :
Result=executor. Submit (get_pic_by_ur)
Result_list. Append (result)
Wait (result_list)
Executor. Shutdown (wait=True)

C=c + 1
If c % 100==0:
Print (STR (datetime. Now ()) + ", "+ STR (c))

  • Related