Python源码示例:queue.join()
示例1
def download_file(url, saveDir):
# interfacelift returns a 403 forbidden unless you include a referer.
headers = { 'User-Agent' : "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)",
'Referer': url}
req = Request(url, None, headers)
filename = IMG_FILE_PATTERN.search(url).group()
saveFile = os.path.join(saveDir, filename)
with open(saveFile, 'wb') as f:
try:
res = urlopen(req)
f.write(res.read())
print('[+] Downloaded %s' % filename)
except Exception as e:
print(e)
try: os.remove(saveFile)
except: pass
# Thread worker. Constantly takes URLs from the queue
示例2
def print_resolution_list():
print('\n[Widescreen 16:10]')
print(', '.join(key for key in RES_WIDESCREEN_16_10))
print('\n[Widescreen 16:9]')
print(', '.join(key for key in RES_WIDESCREEN_16_9))
print('\n[Widescreen 21:9]')
print(', '.join(key for key in RES_WIDESCREEN_21_9))
print('\n[Dual Monitors]')
print(', '.join(key for key in RES_DUAL_MONITORS))
print('\n[Triple Monitors]')
print(', '.join(key for key in RES_TRIPLE_MONITORS))
print('\n[iPhone resolutions]')
print(', '.join(key for key in RES_IPHONE))
print('\n[iPad resolutions]')
print(', '.join(key for key in RES_IPAD))
# Validates the supplied arguments
示例3
def copy_output_file(src_name, src_dir, dst, trim_header, append=True):
src = os.path.join(src_dir, src_name)
dst = os.path.join(os.getcwd(), dst)
logging.debug("Copying {0} to {1}".format(src, dst))
try:
dst_exists = os.path.isfile(dst)
src_file = open(src, 'r', encoding='utf8')
mode = 'w' if not dst_exists or not append else 'a'
dst_file = open(dst, mode, encoding='utf8')
line_count = 0
for line in src_file:
line_count += 1
if line_count == 1 and trim_header and dst_exists:
continue
dst_file.write(line)
src_file.close()
dst_file.close()
except IOError as e:
logging.debug("Exception while copying files: " + str(e))
return
示例4
def copy_files_to_zip(self, dst_file_name, src_dir, is_logs):
dst = os.path.join(os.getcwd(), dst_file_name)
mode = 'w' if not os.path.isfile(dst) else 'a'
optional_dir_name = self.test_config.config_file.replace('.', '_')
if is_logs is True:
log_dir = os.path.join(src_dir, optional_dir_name)
glob_path = glob.glob(os.path.join(log_dir, '*.txt'))
glob_path.extend(glob.glob(os.path.join(log_dir, '*.log')))
glob_path.extend(glob.glob(os.path.join(log_dir, 'crashdumps/*')))
else:
glob_path = glob.glob(os.path.join(src_dir, 'actual.*'))
with zipfile.ZipFile(dst, mode, zipfile.ZIP_DEFLATED) as myzip:
for actual in glob_path:
path = pathlib.PurePath(actual)
file_to_be_zipped = path.name
inner_output = os.path.join(optional_dir_name, file_to_be_zipped)
myzip.write(actual, inner_output)
示例5
def do_test_queue_work(i, q):
"""This will be called in a queue.join() context, so make sure to mark all work items as done and
continue through the loop. Don't try and exit or return from here if there are still work items in the queue.
See the python queue documentation."""
abort_test_run = False
while True:
# This blocks if the queue is empty.
work = q.get()
work.run()
q.task_done()
示例6
def copy_test_result_file(self):
src = os.path.join(self.temp_dir, "tdvt_output.json")
dst = os.path.join(os.getcwd(), TestOutputFiles.output_json)
try:
if not os.path.isfile(dst):
shutil.copyfile(src, dst)
else:
src_file = open(src, 'r', encoding='utf8')
results = json.load(src_file)
src_file.close()
dst_file = open(dst, 'r', encoding='utf8')
existing_results = json.load(dst_file)
dst_file.close()
existing_results['failed_tests'].extend(results['failed_tests'])
existing_results['successful_tests'].extend(results['successful_tests'])
existing_results['skipped_tests'].extend(results['skipped_tests'])
existing_results['disabled_tests'].extend(results['disabled_tests'])
# Check the newly succeeding tests, and if they are in the existing failed
# test list, remove them from the failed test list since they now succeed
for element in results['successful_tests']:
for failed in existing_results['failed_tests']:
if element['test_name'] == failed['test_name']:
existing_results['failed_tests'].remove(failed)
dst_file = open(dst, 'w', encoding='utf8')
json.dump(existing_results, dst_file)
dst_file.close()
except IOError:
return
示例7
def delete_output_files(root_dir):
for f in TestOutputFiles.all_output_files:
out_file = os.path.join(root_dir, f)
for f in glob.glob(out_file):
if os.path.exists(out_file):
try:
os.unlink(out_file)
except Exception as e:
print(e)
continue
示例8
def flush(self):
"""Forces a flush from the internal queue to the server"""
queue = self.queue
size = queue.qsize()
queue.join()
self.log.debug('successfully flushed {0} items.'.format(size))
示例9
def join(self):
"""Ends the consumer thread once the queue is empty. Blocks execution until finished"""
self.consumer.pause()
self.consumer.join()