Created
March 15, 2018 08:48
-
-
Save shilpavijay/c35e505d357a339dee1d57da731d14a5 to your computer and use it in GitHub Desktop.
MThMTasking - push
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
from multiprocessing import Pool | |
import threading | |
import timeit | |
def func(x): return x**1000 | |
#multiprocessing | |
start = timeit.timeit() | |
p = Pool(4) | |
print(p.map(func,[2,4,6,8])) | |
end = timeit.timeit() | |
#without multiprocessing | |
start1 = timeit.timeit() | |
x=map(func,[2,4,6,8]) | |
print(x) | |
end1 = timeit.timeit() | |
#multithreading | |
start2 = timeit.timeit() | |
inp = [2,4,6,8] | |
threads = [] | |
for num in inp: | |
t = threading.Thread(target=func, args=(num,)) | |
threads.append(t) | |
t.start() | |
end2 = timeit.timeit() | |
print threads | |
print("########################") | |
print("Multiprocessing took: ") | |
print(end-start) | |
print("Multithreading took: ") | |
print(end2 - start2) | |
print("Without multiprocessing: ") | |
print(end1-start1) | |
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
from multiprocessing import Pool, Process | |
import timeit | |
import os | |
# CPU intensive processes. | |
# Creating Multiprocessing POOL: | |
def f(x): return x**100000 | |
start = timeit.timeit() | |
p = Pool(processes=8) | |
ans = p.map(f,[1,2,3,4]) | |
end = timeit.timeit() | |
# print ans | |
# print "Time taken: %s" %(end-start) | |
# Choosing number of processes: | |
# Should run as many processes as the CPU reports cores for optimal performance. | |
# Physical CPU cores - i7 has 4 cores | |
# print(mp.cpu_count()) # # of Virtual cores | |
# Hyperthreading: | |
# For each processor core that is physically present, the operating system addresses two virtual (logical) cores and | |
# shares the workload between them when possible. | |
# numproc = <# of CPU bound processes> * <# of CPU cores> * <Hyperthreading> = 0.5 * 4 * 2 | |
# If num of process is more, execution takes longer time due to synchronization. | |
#Creating PROCESS: | |
def info(title): | |
print title | |
print 'module name:', __name__ | |
if hasattr(os, 'getppid'): # only available on Unix | |
print 'parent process:', os.getppid() | |
print 'process id:', os.getpid() | |
def f(name): | |
info('function f') | |
print 'hello', name | |
if __name__ == '__main__': | |
info('main line') | |
p = Process(target=f, args=('bob',)) | |
p.start() | |
p.join() | |
# 'Pool' can be used when a identical task is to be performed parallely for the inputs | |
# 'Process' is used when tasks are non-identical or when one process spawns various other processes. | |
# PROJECT: used in gss_insights.data_creator.job.util.validate_accounts | |
# FURTHER READING: Queue, Pipes, Synchronization using Locks. | |
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 threading | |
import timeit | |
# Multithreading: | |
# IO intensive processes (Image processing etc) | |
# Keeping a process responsive (Loading a page/ print job and editing a doc on MS word) | |
def func(x,num): | |
print "\nWorker %s" % num | |
print x**10 | |
return | |
threads = [] | |
start = timeit.timeit() | |
for i in range(5): | |
x = 10 | |
t = threading.Thread(target=func, args=(x,i)) | |
threads.append(t) | |
x = x*10 | |
t.start() | |
end = timeit.timeit() | |
print "time taken: %s" %(end-start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment