Created
August 2, 2017 13:44
-
-
Save wamonite/c904c112e991df734333155e25f24f60 to your computer and use it in GitHub Desktop.
Python multiprocessing return value and timeout example
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Python multiprocessing return value and timeout example | |
""" | |
from __future__ import print_function | |
import argparse | |
import sys | |
import multiprocessing | |
from ctypes import c_char_p | |
from time import sleep | |
import random | |
PROC_COUNT = 5 | |
SLEEP_MAX = 10 | |
TERMINATE_THRESHOLD = 5 | |
def proc_func(shared_val): | |
sleep_time = random.randint(1, SLEEP_MAX) | |
print('started ({})'.format(sleep_time)) | |
sleep(sleep_time) | |
print('finished') | |
shared_val['job_command'] = 'sleep {}'.format(sleep_time) | |
def do_mp(): | |
val_list = [] | |
proc_list = [] | |
with multiprocessing.Manager() as manager: | |
for proc_num in range(PROC_COUNT): | |
print('creating {}'.format(proc_num)) | |
val = manager.dict() | |
val_list.append(val) | |
proc = multiprocessing.Process(target = proc_func, name = 'proc{}'.format(proc_num), args = (val,)) | |
proc_list.append(proc) | |
proc.start() | |
join_count = 0 | |
finished_list = [] | |
while len(finished_list) != len(proc_list): | |
for proc in proc_list: | |
if proc.name not in finished_list: | |
proc.join(0.001) | |
if proc.is_alive() and join_count >= TERMINATE_THRESHOLD: | |
proc.terminate() | |
print('terminated {}'.format(proc.name)) | |
finished_list.append(proc.name) | |
if not proc.is_alive(): | |
print('joined {}'.format(proc.name)) | |
finished_list.append(proc.name) | |
sleep(1) | |
join_count += 1 | |
for idx, proc in enumerate(proc_list): | |
print('{}: {}'.format(idx, val_list[idx])) | |
def run(): | |
try: | |
do_mp() | |
except Exception as e: | |
print('Error: ({}) {}'.format(e.__class__.__name__, e), file = sys.stderr) | |
sys.exit(1) | |
except KeyboardInterrupt: | |
pass | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment