Created
February 15, 2022 00:58
-
-
Save amroot/32c7fe9b39725655847a72463d0aba9b to your computer and use it in GitHub Desktop.
playing with spawning a limited number of processes
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 | |
from time import sleep | |
from timeit import default_timer as timer | |
def test(i): | |
print(i) | |
sleep(5.0) | |
start = timer() | |
proc = [] | |
for i in range(25): | |
if len(proc) >= 5: | |
print('waiting for a process to finish') | |
if proc[0].is_alive(): | |
proc[0].join() | |
proc.pop(0) | |
else: | |
p = Process(target=test, args=(i,)) | |
p.start() | |
p | |
proc.append(p) | |
# clean up | |
try: | |
for p in proc: | |
if proc[0].is_alive(): | |
proc[0].join() | |
except: | |
pass | |
end = timer() | |
print(end - start) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment