Skip to content

Instantly share code, notes, and snippets.

@juliusf
Created August 8, 2015 10:08
Show Gist options
  • Save juliusf/f7cc12917e24d8f3f4b7 to your computer and use it in GitHub Desktop.
Save juliusf/f7cc12917e24d8f3f4b7 to your computer and use it in GitHub Desktop.
This is a short example of the python async pool I created for a friend. Maybe it is useful to you too
#!/usr/bin/env python
__author__ = 'jules'
from multiprocessing import Pool
import random
import time
class Job:
"""
Representation of a Job. Used to pass data to the pool and to return the result.
You can add all the information you need to this object
"""
def __init__(self, data, callback):
self.data = data
self.result = 0
def main():
p = Pool(processes=2) # creation of the pool. The number of processes should not exceed the number of physical cpu cores available
while True: # run in a loop forever
try:
job_data = int(raw_input('Input:')) # get a new int from the user
the_job = Job( job_data ) # create a new Job object
p.apply_async(process_job, args=[the_job], callback=callback_function) # perform the task asynchronously, call the callback function after you're done
except ValueError:
print "Not a number" # exception handler for when the user does not enter a valid integer
def process_job(job): # function that gets called in a seperate process
print("Processing job. The given data was: %d" % (job.data))
job.result = random.randint(1,5)
time.sleep(job.result)
return job # it is important that you return the job object because it is given as a parameter to the callback function
def callback_function(job): # the function called after the task is finished
print("Job is done! The result was %s" % (job.result))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment