Skip to content

Commit a3d020e

Browse files
committed
Added 3.14 InterpreterPoolExecutor demo.
1 parent 2957db6 commit a3d020e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#-------------------------------------------------------------------------------
2+
# Name: InterpreterExecutor.py
3+
# Purpose: Showcases the use of extension modules created with Delphi
4+
# with the new in Python 3.14 InterpreterPoolExecutor
5+
# You need python 3.14 to run this demo
6+
# It uses the support module prime_utils which imports
7+
# the delphi created extension module.
8+
# Note that each interpreters has its own GIL and
9+
# they are all running in parallel.
10+
#-------------------------------------------------------------------------------
11+
12+
from concurrent.futures import InterpreterPoolExecutor
13+
from prime_utils import count_primes_in_range
14+
import time
15+
16+
def count_primes(max_num, num_interpreters=4):
17+
chunk_size = max_num // num_interpreters
18+
ranges = [(i, min(i + chunk_size - 1, max_num)) for i in range(2, max_num + 1, chunk_size)]
19+
print(ranges)
20+
21+
total = 0
22+
with InterpreterPoolExecutor(max_workers=num_interpreters) as executor:
23+
results = executor.map(count_primes_in_range, ranges)
24+
total = sum(results)
25+
26+
return total
27+
28+
if __name__ == "__main__":
29+
max_number = 1_000_000
30+
start_time = time.time()
31+
prime_count = count_primes(max_number)
32+
end_time = time.time()
33+
34+
print(f"Count of prime numbers up to {max_number}: {prime_count}")
35+
print(f"Time taken: {end_time - start_time:.2f} seconds")

Modules/DemoModule/prime_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# prime_utils.py
2+
from DemoModule import is_prime
3+
4+
def count_primes_in_range(arange):
5+
return sum(1 for n in range(arange[0], arange[1] + 1) if is_prime(n))

0 commit comments

Comments
 (0)