def upload(to_upload): filename = '/tmp/what_the_duck' S = shelve.open(filename) try: # logger.debug('New records: %s previous: %s' % (len(to_upload), len(S))) for u in to_upload: S[u['_id']] = u if not is_internet_connected(): msg = 'Internet is not connected: cannot upload results.' logger.warning(msg) else: remaining = [] for k in S: remaining.append(S[k]) collection = get_upload_collection() logger.info('Uploading %s test results' % len(remaining)) collection.insert_many(remaining) logger.info('done') for r in remaining: del S[r['_id']] finally: # logger.info('Remaining %s' % (len(S))) S.close()