本文整理汇总了Python中multiprocessing.JoinableQueue.join_thread方法的典型用法代码示例。如果您正苦于以下问题:Python JoinableQueue.join_thread方法的具体用法?Python JoinableQueue.join_thread怎么用?Python JoinableQueue.join_thread使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.JoinableQueue
的用法示例。
在下文中一共展示了JoinableQueue.join_thread方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parallel_for
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import join_thread [as 别名]
def parallel_for(a, cls, args=[], kwargs={}, num_processes=None):
from multiprocessing import Process, JoinableQueue, cpu_count, Pipe
if num_processes is None:
num_processes = cpu_count()
# Note that JoinableQueue uses an integer for tracking locations in the queue.
# Because it's using shared memory it's not terribly flexible and gives annoyingly
# unclear errors if you go over the limit. We'd like the queue to be as large as
# possible so that we can avoid contention, but without allocating a max possible
# size queue unless we need it, thus the calculation below. 32767 is a hard limit.
q = JoinableQueue(maxsize=min(len(a)+num_processes, 2**15 - 1))
output_pipes = [Pipe(duplex=False) for _ in range(num_processes)]
send_pipes = [p for _, p in output_pipes]
recv_pipes = [p for p, _ in output_pipes]
pool = [Process(target=_parallel_for, args=(q, cls, pipe) + tuple(args), kwargs=kwargs)
for pipe in send_pipes]
output_watcher = MultiPipeWatcher(recv_pipes)
try:
for p in pool:
p.start()
output_watcher.start()
for x in a:
q.put(x)
for _ in range(num_processes):
q.put(None) # End markers
q.close()
q.join_thread()
q.join()
for p in pool:
p.join()
output_watcher.flush()
output_watcher.join()
combined_output = output_watcher.merged
return combined_output
except KeyboardInterrupt:
print "Interrupted -- terminating worker processes"
for p in pool:
p.terminate()
for p in pool:
p.join()
raise
示例2: main
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import join_thread [as 别名]
def main(argv):
parser = OptionParser()
group = OptionGroup(parser, 'S3 options')
group.add_option('--bucket', metavar='BUCKET',
help='set bucket')
group.add_option('--insecure', action='store_false', dest='secure',
help='use insecure connection')
group.add_option('--secure', action='store_true', default=True, dest='secure',
help='use secure connection')
parser.add_option_group(group)
group = OptionGroup(parser, 'Source options')
group.add_option('--walk', choices=('filesystem', 'tar'), default='filesystem', metavar='MODE',
help='set walk mode (filesystem or tar)')
parser.add_option_group(group)
group = OptionGroup(parser, 'Put options')
group.add_option('--content-type', metavar='CONTENT-TYPE',
help='set content type')
group.add_option('--gzip', action='store_true',
help='gzip values and set content encoding')
group.add_option('--put', choices=('add', 'stupid', 'update'), default='update', metavar='MODE',
help='set put mode (add, stupid, or update)')
group.add_option('--prefix', default='', metavar='PREFIX',
help='set key prefix')
group.add_option('--resume', action='append', default=[], metavar='FILENAME',
help='resume from log file')
group.add_option('--grant', metavar='GRANT', default=None, choices=CannedACLStrings,
help='A canned ACL policy to be applied to each file uploaded.\nChoices: %s' %
', '.join(CannedACLStrings))
parser.add_option_group(group)
group = OptionGroup(parser, 'Logging options')
group.add_option('--log-filename', metavar='FILENAME',
help='set log filename')
group.add_option('--quiet', '-q', action='count', default=0,
help='less output')
group.add_option('--verbose', '-v', action='count', default=0,
help='more output')
parser.add_option_group(group)
group = OptionGroup(parser, 'Debug and performance tuning options')
group.add_option('--dry-run', action='store_true',
help='don\'t write to S3')
group.add_option('--limit', metavar='N', type=int,
help='set maximum number of keys to put')
group.add_option('--processes', default=8, metavar='PROCESSES', type=int,
help='set number of putter processes')
parser.add_option_group(group)
options, args = parser.parse_args(argv[1:])
logging.basicConfig(filename=options.log_filename, level=logging.INFO + 10 * (options.quiet - options.verbose))
logger = logging.getLogger(os.path.basename(sys.argv[0]))
if len(args) < 1:
logger.error('missing source operand')
return 1
if not options.bucket:
logger.error('missing bucket')
return 1
connection = S3Connection(is_secure=options.secure)
bucket = connection.get_bucket(options.bucket)
del bucket
del connection
start = time.time()
put_queue = JoinableQueue(1024 * options.processes)
stat_queue = JoinableQueue()
walk = {'filesystem': walk_filesystem, 'tar': walk_tar}[options.walk]
walker_process = Process(target=walker, args=(walk, put_queue, args, options))
walker_process.start()
put = {'add': put_add, 'stupid': put_stupid, 'update': put_update}[options.put]
putter_processes = list(islice(repeatedly(Process, target=putter, args=(put, put_queue, stat_queue, options)), options.processes))
for putter_process in putter_processes:
putter_process.start()
statter_process = Process(target=statter, args=(stat_queue, start, options))
statter_process.start()
walker_process.join()
for putter_process in putter_processes:
put_queue.put(None)
put_queue.close()
for putter_process in putter_processes:
putter_process.join()
stat_queue.put(None)
stat_queue.close()
statter_process.join()
put_queue.join_thread()
stat_queue.join_thread()