Created
July 16, 2013 15:30
-
-
Save FGtatsuro/6009784 to your computer and use it in GitHub Desktop.
concurrent in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import math | |
from multiprocessing import Process, Queue, Pool | |
from itertools import izip_longest, islice | |
import cProfile | |
def solve(m): | |
return pow(m, 17) % 3569 == 915 | |
def task(q, num_range): | |
q.put([m for m in num_range if solve(m)]) | |
def task_wo_queue(m): | |
if solve(m): | |
return m | |
def solution1(): | |
limit = int(sys.argv[1]) | |
ans = [m for m in range(limit) if solve(m)] | |
print ans | |
def solution2(): | |
queue = Queue() | |
limit = int(sys.argv[1]) | |
worker_num = int(sys.argv[2]) | |
task_size = int(math.ceil(float(limit) / float(worker_num))) | |
num_ranges = izip_longest(*[iter(range(limit))]*task_size) | |
processes = [Process(group=None, target=task, args=(queue, num_range)) | |
for num_range in num_ranges] | |
for process in processes: | |
process.start() | |
for process in processes: | |
print queue.get() | |
process.join() | |
def solution3(): | |
queue = Queue() | |
limit = int(sys.argv[1]) | |
worker_num = int(sys.argv[2]) | |
task_size = int(math.ceil(float(limit) / float(worker_num))) | |
gen = xrange(limit) | |
processes = [Process( | |
group=None, | |
target=task, | |
args=(queue, islice(gen, task_size * i, task_size * (i + 1)))) | |
for i in xrange(worker_num)] | |
for process in processes: | |
process.start() | |
for process in processes: | |
print queue.get() | |
process.join() | |
def solution4(): | |
limit = int(sys.argv[1]) | |
worker_num = int(sys.argv[2]) | |
pool = Pool(processes=worker_num) | |
for m in xrange(limit): | |
future = pool.apply_async(task_wo_queue, args=(m,)) | |
ans = future.get() | |
if ans: | |
print ans | |
def solution5(): | |
limit = int(sys.argv[1]) | |
worker_num = int(sys.argv[2]) | |
task_size = int(math.ceil(float(limit) / float(worker_num))) | |
pool = Pool(processes=worker_num) | |
target = xrange(limit) | |
result = pool.imap(task_wo_queue, target, task_size) | |
print [m for m in result if m] | |
if __name__ == '__main__': | |
profiler = cProfile.Profile() | |
profiler.runcall(solution5) | |
profiler.print_stats() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment