Last active
November 12, 2019 09:41
-
-
Save Sanix-Darker/b22c11cb7720d9ee9f2361003cfd9dc2 to your computer and use it in GitHub Desktop.
[PYTHON]MultiProcessing_Thread.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Process | |
import time | |
from random import randint | |
def func(pid, number): | |
for i in range(1, 50): | |
time.sleep(5) | |
print('[+] Process -> '+str(pid)+': (' + str(number*i*pid) + ')') | |
# list of all processes, so that they can be killed afterwards | |
all_processes = [] | |
# Let's started 5 processes | |
for i in range(1, 10): | |
process = Process(target=func, args=(i,randint(7,30),)) | |
process.start() | |
the_process = { | |
'id':i, | |
'process':process, # The process | |
'started_at': time.time(), # The started date of the process | |
'timeout':randint(1,25) # The delay of the process execution | |
} | |
all_processes.append(the_process) | |
print("[+] ", the_process, " started.") | |
print("[+] ------------------------------------------------------------") | |
# kill all processes after 0.07s | |
time.sleep(0.07) | |
while len(all_processes) > 0: | |
time.sleep(0.5) | |
for i, process in enumerate(all_processes): | |
if ((time.time() - process['started_at']) >= process['timeout']): | |
process['process'].terminate() | |
print("[+] ", process['id'], " -> ", process, " terminated.") | |
del all_processes[i] # removing the process from the array | |
print("[+] ------------------------------------------------------------") | |
# Example of output: | |
# [+] {'id': 1, 'process': <Process(Process-1, started)>, 'started_at': 1572823873.9561033, 'timeout': 3} started. | |
# [+] {'id': 2, 'process': <Process(Process-2, started)>, 'started_at': 1572823873.956958, 'timeout': 8} started. | |
# [+] {'id': 3, 'process': <Process(Process-3, started)>, 'started_at': 1572823873.9578762, 'timeout': 19} started. | |
# [+] {'id': 4, 'process': <Process(Process-4, started)>, 'started_at': 1572823873.958592, 'timeout': 9} started. | |
# [+] {'id': 5, 'process': <Process(Process-5, started)>, 'started_at': 1572823873.959366, 'timeout': 24} started. | |
# [+] {'id': 6, 'process': <Process(Process-6, started)>, 'started_at': 1572823873.9601746, 'timeout': 22} started. | |
# [+] {'id': 7, 'process': <Process(Process-7, started)>, 'started_at': 1572823873.9609804, 'timeout': 15} started. | |
# [+] {'id': 8, 'process': <Process(Process-8, started)>, 'started_at': 1572823873.9619293, 'timeout': 25} started. | |
# [+] {'id': 9, 'process': <Process(Process-9, started)>, 'started_at': 1572823873.9627466, 'timeout': 2} started. | |
# [+] ------------------------------------------------------------ | |
# [+] 9 -> {'id': 9, 'process': <Process(Process-9, started)>, 'started_at': 1572823873.9627466, 'timeout': 2} terminated. | |
# [+] 1 -> {'id': 1, 'process': <Process(Process-1, started)>, 'started_at': 1572823873.9561033, 'timeout': 3} terminated. | |
# [+] Process -> 2: (32) | |
# [+] Process -> 3: (39) | |
# [+] Process -> 5: (80) | |
# [+] Process -> 4: (96) | |
# [+] Process -> 6: (174) | |
# [+] Process -> 7: (91) | |
# [+] Process -> 8: (96) | |
# [+] 2 -> {'id': 2, 'process': <Process(Process-2, started)>, 'started_at': 1572823873.956958, 'timeout': 8} terminated. | |
# [+] 4 -> {'id': 4, 'process': <Process(Process-4, started)>, 'started_at': 1572823873.958592, 'timeout': 9} terminated. | |
# [+] Process -> 3: (78) | |
# [+] Process -> 8: (192) | |
# [+] Process -> 5: (160) | |
# [+] Process -> 6: (348) | |
# [+] Process -> 7: (182) | |
# [+] Process -> 3: (117) | |
# [+] Process -> 5: (240) | |
# [+] Process -> 8: (288) | |
# [+] Process -> 6: (522) | |
# [+] Process -> 7: (273) | |
# [+] 7 -> {'id': 7, 'process': <Process(Process-7, started)>, 'started_at': 1572823873.9609804, 'timeout': 15} terminated. | |
# [+] 3 -> {'id': 3, 'process': <Process(Process-3, started)>, 'started_at': 1572823873.9578762, 'timeout': 19} terminated. | |
# [+] Process -> 5: (320) | |
# [+] Process -> 6: (696) | |
# [+] Process -> 8: (384) | |
# [+] 6 -> {'id': 6, 'process': <Process(Process-6, started)>, 'started_at': 1572823873.9601746, 'timeout': 22} terminated. | |
# [+] 5 -> {'id': 5, 'process': <Process(Process-5, started)>, 'started_at': 1572823873.959366, 'timeout': 24} terminated. | |
# [+] Process -> 8: (480) | |
# [+] 8 -> {'id': 8, 'process': <Process(Process-8, started)>, 'started_at': 1572823873.9619293, 'timeout': 25} terminated. | |
# [+] ------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment