adds missing python scripts

This commit is contained in:
waldek 2022-08-23 09:59:09 +02:00
parent b2c5e93fe1
commit 8fa1a1dc37
4 changed files with 57 additions and 3 deletions

View File

@ -405,6 +405,6 @@ TODO
To help you understand what happens to running and stopped processes I made a few python scripts you can download below.
Run them either with `python3 $SCRIPT_NAME` or `./$SCRIPT_NAME`.
* [simple timer](./assets/processes_ex_01.py)
* [timer with random keyboard prompt](./assets/processes_ex_02.py)
* [custom callback function for SIGALRM](./assets/processes_ex_03.py)
* [simple timer](../assets/processes_ex_01.py)
* [timer with random keyboard prompt](../assets/processes_ex_02.py)
* [custom callback function for SIGALRM](../assets/processes_ex_03.py)

13
assets/processes_ex_01.py Normal file
View File

@ -0,0 +1,13 @@
import time
import os
start_ts = time.time()
loops = 0
while True:
time.sleep(1)
loops += 1
tick_ts = time.time()
delta = int(tick_ts - start_ts)
pid = os.getpid()
print("I'm {} and I've been running for {} seconds and did {} loops".format(pid, delta, loops))

23
assets/processes_ex_02.py Normal file
View File

@ -0,0 +1,23 @@
import time
import os
import random
start_ts = time.time()
loops = 0
while True:
time.sleep(1)
loops += 1
tick_ts = time.time()
delta = int(tick_ts - start_ts)
pid = os.getpid()
print("I'm {} and I've been running for {} seconds and did {} loops".format(pid, delta, loops))
chance = random.randint(0, 20)
if chance == 5:
response = input("I need some input...")
if len(response) == 0:
exit(0)
else:
print("thanks for the response, I'll reset my counter and continue now...")
start_ts = time.time()
loops = 0

18
assets/processes_ex_03.py Normal file
View File

@ -0,0 +1,18 @@
import time
import os
import signal
def alarm_handler(signum, frame):
print("I'm an alarm hear me ring! (my ID is {})".format(signum))
def main():
print("I'm {}".format(os.getpid()))
while True:
time.sleep(1)
if __name__ == "__main__":
signal.signal(signal.SIGALRM, alarm_handler)
try:
main()
except Exception as e:
print(e)