Skip to content

Instantly share code, notes, and snippets.

@JohnStarich
Created July 13, 2017 01:10
Show Gist options
  • Save JohnStarich/7210483ee23bf2fda7d3324b0d1a7ba0 to your computer and use it in GitHub Desktop.
Save JohnStarich/7210483ee23bf2fda7d3324b0d1a7ba0 to your computer and use it in GitHub Desktop.
A device simulator. Devices must not be run concurrently, but all jobs should be run in a distributed fashion.
#!/usr/bin/env python3
# This solution randomly assigns each job to a device
# and then groups jobs by device ID. Each of these groups
# is then submitted to a proccessing pool. Whichever worker
# finishes first grabs the next group to process.
from __future__ import print_function
from multiprocessing.pool import Pool
import random
import requests
class Job(object):
def __init__(self, device):
self.device = device
def run(self):
r = requests.get('http://google.com')
print('[Device %d] http://google.com request completed' % self.device)
return self.device
def worker_func(jobs):
results = []
for job in jobs:
results.append(job.run())
return results
if __name__ == '__main__':
pool = Pool(3)
job_count = 100
jobs = {}
devices = range(10)
for device in devices:
jobs[device] = []
for _ in range(job_count):
device = random.choice(devices)
job = Job(device)
jobs[device].append(job)
results = list(pool.map(worker_func, jobs.values()))
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment