-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathU7_3_PrimRechner_Multiprocessing.py
More file actions
executable file
·55 lines (47 loc) · 1.42 KB
/
U7_3_PrimRechner_Multiprocessing.py
File metadata and controls
executable file
·55 lines (47 loc) · 1.42 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
54
##############################################
#
# Name: U7_2_PrimRechner_Multiprocessing.py
#
# Author: Peter Christen
#
# Version: 1.1
#
# Date: 10.07.2017
# Change: 31.03.2021 : V1.1 : Anpassungen für neue Python Versionen
#
# Purpose: Verteilt eine Berechnung von Primzahlen auf mehrere Prozesse
#
##############################################
from multiprocessing import Process, Pipe
from time import perf_counter
# Variabeln
pc = [] # Liste für Primzahlenzähler
pia, pib = Pipe() # Pipe für Primzahlen erstellen
# Funktionen
def primrechner(ps, pe, pia):
# Berechnung erstellen
print("Suche Primzahlen von", ps, "bis", pe)
for z in range(ps, pe + 1):
pc.append(z)
for z2 in range(2, z):
if not z % z2:
pc.remove(z)
break
pia.send(len(pc))
pia.close()
if __name__ == '__main__':
start = perf_counter()
# Prozesse starten
px = Process(target=primrechner, args=(1, 17000, pia))
px.start()
px = Process(target=primrechner, args=(17001, 24000, pia))
px.start()
px = Process(target=primrechner, args=(24001, 30000, pia))
px.start()
# Abschluss
anzahlprimzahlen = pib.recv()
anzahlprimzahlen = anzahlprimzahlen + pib.recv()
anzahlprimzahlen = anzahlprimzahlen + pib.recv()
print(f"Es wurden {anzahlprimzahlen} Primzahlen gefunden")
end = perf_counter()
print(f"Performance: {end - start} Sec")