summaryrefslogtreecommitdiff
path: root/primes.py
diff options
context:
space:
mode:
authorbrian.quinlan <devnull@localhost>2009-05-04 21:02:48 +0000
committerbrian.quinlan <devnull@localhost>2009-05-04 21:02:48 +0000
commita12b38ce97773a42379c032cb4003411c094c030 (patch)
tree48b6a806f5e7320eec8cd4fe15b5a55cb570ff67 /primes.py
parent3b24ddaa96d138bac457f5d394c3a71947ad2d15 (diff)
downloadfutures-a12b38ce97773a42379c032cb4003411c094c030.tar.gz
First maybe-working version of process pools.
Diffstat (limited to 'primes.py')
-rw-r--r--primes.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/primes.py b/primes.py
new file mode 100644
index 0000000..4397c67
--- /dev/null
+++ b/primes.py
@@ -0,0 +1,46 @@
+import futures
+import math
+import time
+
+PRIMES = [
+ 112272535095293,
+ 112582705942171,
+ 112272535095293,
+ 115280095190773,
+ 115797848077099]
+
+def is_prime(n):
+ n = abs(n)
+ i = 2
+ while i <= math.sqrt(n):
+ if n % i == 0:
+ return False
+ i += 1
+ return True
+
+def sequential():
+ return list(map(is_prime, PRIMES))
+
+def with_process_pool_executor():
+ executor = futures.ProcessPoolExecutor()
+ try:
+ return list(executor.map(is_prime, PRIMES))
+ finally:
+ executor.shutdown()
+
+def with_thread_pool_executor():
+ executor = futures.ThreadPoolExecutor(10)
+ try:
+ return list(executor.map(is_prime, PRIMES))
+ finally:
+ executor.shutdown()
+
+def main():
+ for name, fn in [('sequential', sequential),
+ ('processes', with_process_pool_executor),
+ ('threads', with_thread_pool_executor)]:
+ start = time.time()
+ fn()
+ print('%s: %.2f seconds' % (name.ljust(10), time.time() - start))
+
+main() \ No newline at end of file