Skip to content

Instantly share code, notes, and snippets.

@ostollmann
Created March 6, 2012 00:21
Show Gist options
  • Save ostollmann/1982416 to your computer and use it in GitHub Desktop.
Save ostollmann/1982416 to your computer and use it in GitHub Desktop.
import time
import multiprocessing
from multiprocessing import Queue
q = Queue()
class Worker(multiprocessing.Process):
def __init__(self,uid,workQueue):
multiprocessing.Process.__init__(self)
self.uid = uid
self.workQueue = workQueue
def run(self):
while True:
jobName,jobData = self.workQueue.get()
if jobName == "EXIT":
return
elif jobName == "MULTIPLY":
result = jobData[0]*jobData[1]
#print("Result[%s]: %s=%s" % (self.uid,jobName,result))
elif jobName == "ADD":
result = jobData[0]+jobData[1]
#print("Result[%s]: %s=%s" % (self.uid,jobName,result))
elif jobName == "SQUARE":
for x in xrange(1000):
result = jobData[0]*jobData[0]
#print("Result[%s]: %s(%s)=%s" % (self.uid,jobName,jobData[0],result))
else:
print("ERROR: wrong jobName")
class Manager(object):
def __init__(self,numWorkers=100):
self.numWorkers = numWorkers
self.workers = []
self.workQueue = Queue()
self.startWorkers()
def startWorkers(self):
for w in xrange(self.numWorkers):
w = Worker(w,self.workQueue)
w.start()
self.workers.append(w)
def stopWorkers(self):
for w in self.workers:
w.terminate()
def multiply(self,a,b):
job = ("MULTIPLY",(a,b))
self.workQueue.put(job)
def add(self,a,b):
job = ("ADD",(a,b))
self.workQueue.put(job)
def square(self,a):
job = ("SQUARE",(a,))
self.workQueue.put(job)
if __name__ == "__main__":
import sys,time
t = time.time()
m = Manager(int(sys.argv[1]))
for r in xrange(int(sys.argv[2])):
m.square(r)
print("elapsed: %s" % (time.time() - t))
m.stopWorkers()
sys.exit()
import time
import multiprocessing
from multiprocessing import Queue
q = Queue()
class Worker(multiprocessing.Process):
def __init__(self,uid,workQueue):
multiprocessing.Process.__init__(self)
self.uid = uid
self.workQueue = workQueue
def run(self):
while True:
jobName,jobData = self.workQueue.get()
if jobName == "EXIT":
return
elif jobName == "MULTIPLY":
result = jobData[0]*jobData[1]
#print("Result[%s]: %s=%s" % (self.uid,jobName,result))
elif jobName == "ADD":
result = jobData[0]+jobData[1]
#print("Result[%s]: %s=%s" % (self.uid,jobName,result))
elif jobName == "SQUARE":
for x in xrange(1000):
result = jobData[0]*jobData[0]
#print("Result[%s]: %s(%s)=%s" % (self.uid,jobName,jobData[0],result))
else:
print("ERROR: wrong jobName")
class Manager(object):
def __init__(self,numWorkers=100):
self.numWorkers = numWorkers
self.workers = []
self.workQueue = Queue()
self.startWorkers()
def startWorkers(self):
for w in xrange(self.numWorkers):
w = Worker(w,self.workQueue)
w.start()
self.workers.append(w)
def stopWorkers(self):
for w in self.workers:
w.terminate()
def multiply(self,a,b):
job = ("MULTIPLY",(a,b))
self.workQueue.put(job)
def add(self,a,b):
job = ("ADD",(a,b))
self.workQueue.put(job)
def square(self,a):
job = ("SQUARE",(a,))
self.workQueue.put(job)
if __name__ == "__main__":
import sys,time
t = time.time()
m = Manager(int(sys.argv[1]))
for r in xrange(int(sys.argv[2])):
m.square(r)
print("elapsed: %s" % (time.time() - t))
m.stopWorkers()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment