I have created three functions where on the first I have created a countdown timer and on the other two I find prime numbers. I am using multiprocessing
to make them execute together. What I am trying to make it do is when the timer finishes the other two process will also terminate. The other two functions will keep searching for the prime numbers until the timer finishes e.g. if timer is set to 1 minute, the functions will keep searching for prime numbers for one minute.
Right now the code waits n seconds before checking for the next number. What I did is I passed from the countdown function to the other two the seconds when they reach 0 to terminate the loop inside the other two functions.
Here is my code:
import time import sys from multiprocessing import Process def countdown(t): while t: mins, secs = divmod(t, 60) timer = '{:02d}:{:02d}'.format(mins, secs) print(timer, end="\r") time.sleep(1) t -= 1 def isPrime1(): f = open('results.txt','w') i = 2 while countdown(t) != 0: c = 0; for j in range (1, (i + 1), 1): a = i % j if (a == 0): c = c + 1 if c == 2: f.write(str(i)) print(i) if countdown(t) == 0: break i = i + 1 f.close() def isPrime2(): f = open('results1.txt','w') i = 2 while countdown(t) != 0: mins, secs = divmod(t, 60) c = 0; for j in range (1, (i + 1), 1): a = i % j if (a == 0): c = c + 1 if c == 2: f.write(str(i)) print(i) if countdown(t) == 0: break i = i + 1 f.close() if __name__=='__main__': t = int(input("Seconds:")) p1 = Process(target=isPrime1()) p1.start() p2 = Process(target=isPrime2()) p2.start() p3 = Process(target=countdown(t)) p3.start()
https://stackoverflow.com/questions/66619888/how-to-terminate-a-process-when-other-process-finishes March 14, 2021 at 08:24AM
没有评论:
发表评论