-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprozesse.py
More file actions
executable file
·44 lines (34 loc) · 959 Bytes
/
prozesse.py
File metadata and controls
executable file
·44 lines (34 loc) · 959 Bytes
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
#!/usr/bin/env python3
from multiprocessing import Process
from time import sleep
from os import getpid
from sys import argv
"""
multiprocessing ist programmatisch ähnlich zu verwenden wie threading.
Das Modul heißt anders und die Klasse auch (nämlich Process).
"""
class PIDPrinter(Process):
def __init__(self, wait: float = 0.1) -> None:
Process.__init__(self)
self.wait = wait
self.daemon = True # siehe Threads
def run(self) -> None:
while True:
print(getpid())
sleep(self.wait)
"""
Alternativ kann man auch einfach eine bestimmte Methode
in einem neuen Prozess ausführen ohne eine neue Klasse zu schreiben:
def fun() -> None:
pass
Process(target=fun).start()
"""
if len(argv) > 1:
count = int(argv[1])
else:
count = 2
for x in range(count):
PIDPrinter().start()
while True:
sleep(100)
# siehe auch: https://docs.python.org/3.6/library/multiprocessing.html