Created
February 9, 2016 13:05
-
-
Save reox/d2491d0f8d6da280ccb4 to your computer and use it in GitHub Desktop.
A Stub for multiprocessing, also handles Ctrl+C correctly and kills childs
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
import sys | |
from argparse import ArgumentParser | |
from multiprocessing import Queue, Array | |
import multiprocessing | |
import signal | |
import os | |
import time | |
# this signal handler terminates the child-process on SIGINT or SIGTERM event | |
def sighandler(signal, frame): | |
os._exit(0) | |
# worker thread definiton | |
def worker(id): | |
# register handlers for SIGINT and SIGTERM | |
signal.signal(signal.SIGINT, sighandler) | |
signal.signal(signal.SIGTERM, sighandler) | |
# Stub code! | |
time.sleep(100) | |
print("Hello this is Worker ID", id) | |
def main(): | |
parser = ArgumentParser(description='Do something multiprocessed.') | |
parser.add_argument('-t', '--threads', metavar='N', type=int, help='Number of Threads to start, default 8', default=8) | |
args = parser.parse_args() | |
print("Starting up %d processes:" % args.threads) | |
# Store the processes for later | |
procs = [] | |
for i in range(args.threads): | |
sys.stdout.write("\r\tStarting Worker Process: %d" %(i+1)) | |
sys.stdout.flush() | |
p = multiprocessing.Process(target=worker, args=[i]) | |
procs.append(p) | |
p.start() | |
sys.stdout.write("\n") | |
print("All processes started...") | |
try: | |
# Wait for the processes to stop | |
for p in procs: | |
p.join() | |
except KeyboardInterrupt as e: | |
# no need to terminate something here, because the processes should killed them self (sighandler) | |
sys.exit(0) | |
print("done working") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment