Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: webrtc/tools/barcode_tools/helper_functions.py

Issue 1428433002: Rewrote perform_action_on_all_files to be parallell. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 # 3 #
4 # Use of this source code is governed by a BSD-style license 4 # Use of this source code is governed by a BSD-style license
5 # that can be found in the LICENSE file in the root of the source 5 # that can be found in the LICENSE file in the root of the source
6 # tree. An additional intellectual property rights grant can be found 6 # tree. An additional intellectual property rights grant can be found
7 # in the file PATENTS. All contributing project authors may 7 # in the file PATENTS. All contributing project authors may
8 # be found in the AUTHORS file in the root of the source tree. 8 # be found in the AUTHORS file in the root of the source tree.
9 9
10 import multiprocessing
10 import os 11 import os
11 import subprocess 12 import subprocess
12 import sys 13 import sys
13 14
14 _DEFAULT_PADDING = 4 15 _DEFAULT_PADDING = 4
15 16
16 17
17 class HelperError(Exception): 18 class HelperError(Exception):
18 """Exception raised for errors in the helper.""" 19 """Exception raised for errors in the helper."""
19 pass 20 pass
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 '%s and %s' % (' '.join(cmd_list), process.returncode, 58 '%s and %s' % (' '.join(cmd_list), process.returncode,
58 output, error)) 59 output, error))
59 return output.strip() 60 return output.strip()
60 61
61 62
62 def perform_action_on_all_files(directory, file_pattern, file_extension, 63 def perform_action_on_all_files(directory, file_pattern, file_extension,
63 start_number, action, **kwargs): 64 start_number, action, **kwargs):
64 """Function that performs a given action on all files matching a pattern. 65 """Function that performs a given action on all files matching a pattern.
65 66
66 It is assumed that the files are named file_patternxxxx.file_extension, where 67 It is assumed that the files are named file_patternxxxx.file_extension, where
67 xxxx are digits. The file names start from 68 xxxx are digits starting from start_number.
68 file_patern0..start_number>.file_extension.
69 69
70 Args: 70 Args:
71 directory(string): The directory where the files live. 71 directory(string): The directory where the files live.
72 file_pattern(string): The name pattern of the files. 72 file_pattern(string): The name pattern of the files.
73 file_extension(string): The files' extension. 73 file_extension(string): The files' extension.
74 start_number(int): From where to start to count frames. 74 start_number(int): From where to start to count frames.
75 action(function): The action to be performed over the files. Must return 75 action(function): The action to be performed over the files. Must return
76 False if the action failed, True otherwise. 76 False if the action failed, True otherwise. It should take a file name
77 as the first argument and **kwargs as arguments. The function must be
78 possible to pickle, so it cannot be a bound function (for instance).
77 79
78 Return: 80 Return:
79 (bool): Whether performing the action over all files was successful or not. 81 (bool): Whether performing the action over all files was successful or not.
80 """ 82 """
81 file_prefix = os.path.join(directory, file_pattern) 83 file_prefix = os.path.join(directory, file_pattern)
82 file_exists = True
83 file_number = start_number 84 file_number = start_number
84 errors = False
85 85
86 while file_exists: 86 process_pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
87 results = []
88 while True:
87 zero_padded_file_number = zero_pad(file_number) 89 zero_padded_file_number = zero_pad(file_number)
88 file_name = file_prefix + zero_padded_file_number + '.' + file_extension 90 file_name = file_prefix + zero_padded_file_number + '.' + file_extension
89 if os.path.isfile(file_name): 91 if not os.path.isfile(file_name):
90 if not action(file_name=file_name, **kwargs): 92 break
91 errors = True 93 future = process_pool.apply_async(action, args=(file_name,), kwds=kwargs)
92 break 94 results.append(future)
93 file_number += 1 95 file_number += 1
94 else: 96
95 file_exists = False 97 successful = True
96 return not errors 98 for result in results:
99 if not result.get():
100 print "At least one action %s failed for files %sxxxx.%s." % (
101 action, file_pattern, file_extension)
102 successful = False
103
104 process_pool.close()
105 return successful
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698