Last active
January 7, 2019 19:08
-
-
Save rootux/ffdf9e874a7f4d25792efbf25b878429 to your computer and use it in GitHub Desktop.
Revisions
-
rootux revised this gist
Jan 7, 2019 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ import os import sys import random @@ -24,7 +25,7 @@ def sub_program(): sys.exit() number_of_processes = sys.argv[1] if not is_number(number_of_processes): print("Error - Please supply number as the number of processes") sys.exit() @@ -61,4 +62,4 @@ def sub_program(): finally: print("Finished running.\r\nTime {}".format(datetime.now())) os.unlink(pidfile) -
rootux revised this gist
Jan 7, 2019 . 1 changed file with 13 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ from multiprocessing import Process from utils import is_number MAX_TIME_TO_SLEEP = 20 def sub_program(): pid = str(os.getpid()) @@ -24,7 +24,7 @@ def sub_program(): sys.exit() number_of_processes = sys.argv[1] if not is_number(number_of_processes): print("Error - Please supply number as the number of processes") sys.exit() @@ -38,7 +38,7 @@ def sub_program(): sys.exit() open(pidfile, "w").write("{}".format(parent_id)) try: processes = [] for i in range(number_of_processes): @@ -48,10 +48,17 @@ def sub_program(): for p in processes: p.start() success = 0 failure = 0 for p in processes: p.join() if p.exitcode == 0: success+=1 else: failure+=1 print("Success process {}, Failed process {}".format(success,failure)) finally: print("Finished running.\r\nTime {}".format(datetime.now())) os.unlink(pidfile) -
rootux created this gist
Jan 6, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ import os import sys import random import time import tempfile from datetime import datetime from multiprocessing import Process from utils import is_number MAX_TIME_TO_SLEEP = 10 def sub_program(): pid = str(os.getpid()) sleep_time = random.randrange(MAX_TIME_TO_SLEEP) print("Hello from pid {}\r\nTime {}".format(pid, datetime.now())) print("Sleeping for {} seconds".format(sleep_time)) time.sleep(sleep_time) if __name__ == '__main__': parent_id = os.getpid() pidfile = os.path.join(tempfile.gettempdir(),"my_unique_process.pid") if(len(sys.argv) <= 1): print("Error - Please supply number of processes as an argument. For example {} 3".format(sys.argv[0])) sys.exit() number_of_processes = sys.argv[1] if not is_number(number_of_processes): print("Error - Please supply number as the number of processes") sys.exit() number_of_processes = int(number_of_processes) print("Started at time {}".format(datetime.now())) # Check if already running if os.path.isfile(pidfile): print("{} already exists, exiting".format(pidfile)) sys.exit() open(pidfile, "w").write("{}".format(parent_id)) try: processes = [] for i in range(number_of_processes): p = Process(target=sub_program) processes.append(p) for p in processes: p.start() for p in processes: p.join() finally: print("Finished running.\r\nTime {}".format(datetime.now())) os.unlink(pidfile)