2016-07-25 31 views
1

2700個以上のファイルを処理する際に問題があります これは数百のファイルが少しありますが、これはウィンドウが開いていることと関係がありますlinux ulimitのようなファイルは、システム全体で定義することができます。私は物事が閉じられていないと確信している、これが私がこのエラーを取得している理由です。開いているファイルが多すぎます。urllib

私はポストを経由してファイルを送信機能があります:あなたが番号を変更するためにCで_setmaxstdioを設定することができます

for photo_path in [p.lower() for p in photos_path]: 
     if ('jpg' in photo_path or 'jpeg' in photo_path) and "thumb" not in photo_path: 
      nr_photos_upload +=1 
    print("Found " + str(nr_photos_upload) + " pictures to upload") 
    local_count = 0 
    list_to_upload = [] 
    for photo_path in [p.lower() for p in photos_path]: 
     local_count += 1 
     if ('jpg' in photo_path or 'jpeg' in photo_path) and "thumb" not in photo_path and local_count > count: 
      total_img = nr_photos_upload 
      photo_name = os.path.basename(photo_path) 
      try : 
       photo = {'photo': (photo_name, open(path + photo_path, 'rb'), 'image/jpeg')} 
       try: 
        latitude, longitude, compas = get_gps_lat_long_compass(path + photo_path) 
       except ValueError as e: 
        if e != None: 
         try: 
          tags = exifread.process_file(open(path + photo_path, 'rb')) 
          latitude, longitude = get_exif_location(tags) 
          compas = -1 
         except Exception: 
          continue 
       if compas == -1: 
        data_photo = {'coordinate' : str(latitude) + "," + str(longitude), 
           'sequenceId'  : id_sequence, 
           'sequenceIndex' : count 
           } 
       else : 
        data_photo = {'coordinate' : str(latitude) + "," + str(longitude), 
           'sequenceId'  : id_sequence, 
           'sequenceIndex' : count, 
           'headers'   : compas 
           } 
       info_to_upload = {'data': data_photo, 'photo':photo, 'name': photo_name} 
       list_to_upload.append(info_to_upload) 
       count += 1 
      except Exception as ex: 
       print(ex) 
    count_uploaded = 0 
    with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: 
     # Upload feature called from here 
     future_to_url = {executor.submit(upload_photos, url_photo, dict, 100): dict for dict in list_to_upload} 
     for future in concurrent.futures.as_completed(future_to_url): 
      try: 
       data = future.result()['json'] 
       name = future.result()['name'] 
       print("processing {}".format(name)) 
       if data['status']['apiCode'] == "600": 

        percentage = float((float(count_uploaded) * 100)/float(total_img)) 
        print(("Uploaded - " + str(count_uploaded) + ' of total :' + str(
         total_img) + ", percentage: " + str(round(percentage, 2)) + "%")) 
       elif data['status']['apiCode'] == "610": 
        print("skipping - a requirement arguments is missing for upload") 
       elif data['status']['apiCode'] == "611": 
        print("skipping - image does not have GPS location metadata") 
       elif data['status']['apiCode'] == "660": 
        print("skipping - duplicate image") 
       else : 
        print("skipping - bad image") 
       count_uploaded += 1 
       with open(path + "count_file.txt", "w") as fis: 
        fis.write((str(count_uploaded))) 
      except Exception as exc: 
       print('%generated an exception: %s' % (exc)) 
+0

ので、問題は非常に多くのファイルのsimulatanious処理です。何か問題があれば停止しないで、数ミリ秒待って繰り返してください(無限ループを避けるように注意してください)。この場合、すべてのファイルが処理されます。 – Ilya

+0

一般的に、特に同じサーバーを叩いているときに、同時に複数のファイルを同時にアップロードすることはあまり有益ではありません。同時接続数を減らしてください。 –

答えて

1

def upload_photos(url_photo, dict, timeout): 
    photo = dict['photo'] 
    data_photo = dict['data'] 
    name = dict['name'] 
    conn = requests.post(url_photo, data=data_photo, files=photo, timeout=timeout) 
    return {'json': conn.json(), 'name': name} 

ディレクトリリストのループから呼び出されますファイルを一度に開くことができます。デフォルトは512ある

import win32file 
win32file._setmaxstdio(1024) #set max number of files to 1024 

:Python用

あなたがようpywin32からwin32fileを使用する必要があります。また、設定した最大値がプラットフォームでサポートされていることを確認してください。

参考:https://msdn.microsoft.com/en-us/library/6e3b887c.aspx

+1

"開いているファイルの最大数"は、あなたのアルゴリズムに疑問を投げかけ、限界を変更する方法を見つけられないように、今のところ十分な上限です... –

+1

このオプションは動作しません。このようなことをするベストプラクティス – James

関連する問題