Skip to content

Instantly share code, notes, and snippets.

@sligodave
Created June 22, 2012 20:05
Show Gist options
  • Save sligodave/2974835 to your computer and use it in GitHub Desktop.
Save sligodave/2974835 to your computer and use it in GitHub Desktop.
Keep five process running at almost all time for one minute.
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