Created
June 22, 2012 20:05
-
-
Save sligodave/2974835 to your computer and use it in GitHub Desktop.
Keep five process running at almost all time for one minute.
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 multiprocessing | |
import time | |
from os import getpid | |
def worker(): | |
pid = getpid() | |
print 'START %d' % pid | |
time.sleep(3) | |
print 'STOP %d' % pid | |
if __name__ == '__main__': | |
from datetime import datetime, timedelta | |
concurrent_jobs_count = 5 | |
minutes_to_run = 1 | |
end_time = datetime.now() + timedelta(minutes=minutes_to_run) | |
jobs = [] | |
# We only run for the time specified | |
while datetime.now() < end_time: | |
# Check all processes to see which are still running | |
jobs = [x for x in jobs if x.is_alive()] | |
# Create new processes to replace finished processes | |
while len(jobs) < concurrent_jobs_count: | |
jobs.append(multiprocessing.Process(target=worker)) | |
jobs[-1].start() | |
# Wait, just so this isn't looping all the time | |
time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment