-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathU7_4_PrimRechner_Pool.py
More file actions
executable file
·53 lines (44 loc) · 1.21 KB
/
U7_4_PrimRechner_Pool.py
File metadata and controls
executable file
·53 lines (44 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
##############################################
#
# Name: U7_4_PrimRechner_Pool.py
#
# Author: Peter Christen
#
# Version: 1.0
#
# Date: 10.08.2024
#
# Purpose: Errechnet die Primzahlen innerhalb eines definierten Zahlenbereiches
#
##############################################
# Module
from time import perf_counter
from multiprocessing import Pool,Pipe
# Variabeln
pc = [] # Liste für Primzahlenzähler
pia, pib = Pipe() # Pipe für Primzahlen erstellen
# Funktionen
def primrechner(data):
print("Suche Primzahlen von", data[0], "bis", data[1])
for z in range(data[0], data[1] + 1):
pc.append(z)
for z2 in range(2, z):
if not z % z2:
pc.remove(z)
break
data[2].send(len(pc))
#data[2].close()
def pool_handler():
p = Pool(3)
p.map(primrechner, [(1,17000,pia),(17001,24000,pia),(24001,30000,pia)])
if __name__ == '__main__':
# Prozess starten
start = perf_counter()
pool_handler()
# Abschluss
anzahlprimzahlen=0
while pib.poll():
anzahlprimzahlen = anzahlprimzahlen + pib.recv()
print(f"Es wurden {anzahlprimzahlen} Primzahlen gefunden")
end = perf_counter()
print(f"Performance: {end - start} Sec")