Aspected time
50 Min
Aspected time
30 Min
Aspected time
15 Min
Parallelism is to add more hardware or software resources to make computation faster.
Parallelism is about doing lots of things at once
Permit multiple tasks to proceed without waiting for each other.
Concurrency is about dealing with lots of things at once
from threading import Thread
def print_hello():
print("Hello World")
t = Thread(target=print_hello, args=[])
t.start()
t.join()
from threading import Thread
def calc(n):
while n > 0:
n -= 1
t = Thread(target=calc, args=[100000000])
t.start()
t.join()
# Started a single thread
# 4.02 seconds to execute this program
from threading import Thread
def calc(n):
while n > 0:
n -= 1
t1 = Thread(target=calc, args=[100000000])
t2 = Thread(target=calc, args=[100000000])
t1.start()
t2.start()
t1.join()
t2.join()
# How much time this program would take to
# complete execution ?
from multiprocessing import Process
from threading import Thread
def calc(n):
while n > 0:
n -= 1
def do_calc():
t = Thread(target=calc, args=[100000000])
t.start()
t.join()
p1 = Process(target=do_calc, args=[])
p2 = Process(target=do_calc, args=[])
p1.start()
p2.start()
p1.join()
p2.join()
OS
from multiprocessing.pool import ThreadPool
def f(x):
return x*x
if __name__ == '__main__':
p = ThreadPool(4)
print(p.map(f, [1, 2, 3, 4, 5, 6]))
# Output
[1, 4, 9, 16, 25, 36]
from multiprocessing import Process
def f(name):
print 'hello world'
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
p = Pool(5)
print(p.map(f, [1, 2, 3, 4, 5, 6]))
def getPart1():
lock.acquire()
try:
... get first part of the data
finally:
lock.release()
def getPart2():
lock.acquire()
try:
... get first part of the data
finally:
lock.release()
import time
from threading import Thread, Semaphore
semaphore = Semaphore(5)
def calc():
semaphore.acquire()
time.sleep(3)
# Send http request to http://hitul.in
semaphore.release()
for _ in range(10):
t1 = Thread(target=calc, args=[])
t1.start()
t1.join()
import time
import requests
from threading import Thread, BoundedSemaphore
semaphore = BoundedSemaphore(3)
def send_request():
semaphore.acquire()
r = requests.get("http://hitul.in/")
time.sleep(3)
print("Request sent")
semaphore.release()
# Value error
semaphore.release()
for _ in range(10):
t1 = Thread(target=send_request, args=[])
t1.start()
t1.join()
import time
from threading import Thread, Event
event = Event()
def hello():
event.wait()
print("Hello world")
t1 = Thread(target=hello, args=[])
t1.start()
t2 = Thread(target=hello, args=[])
t2.start()
time.sleep(5)
event.set()
event.clear()
t1.join()
t2.join()
from threading import Timer
def hello():
print("Hello World")
t = Timer(2, hello)
t.start()
from threading import Thread
data = {}
def print_hello():
data["Status"] = True
t = Thread(target=print_hello, args=[])
t.start()
t.join()
print data
from multiprocessing import Process
data = {}
def print_hello():
data["Status"] = True
p = Process(target=print_hello, args=[])
p.start()
p.join()
print data
import time
from multiprocessing import Pipe, Process
c1, c2 = Pipe()
def write_pipe(c2):
c2.send("Hello world")
def read_pipe(c1):
print c1.recv()
p1 = Process(target=write_pipe, args=[c1])
p2 = Process(target=read_pipe, args=[c2])
p1.start()
time.sleep(1)
p2.start()
p1.join()
p2.join()
import time
from multiprocessing import Pipe, Process
c1, c2 = Pipe()
def write_pipe(c2):
c2.send("Hey! How are you ?")
print "MSG by read-process : %s" % c2.recv()
def read_pipe(c1):
print "MSG by write-process function : %s" % c1.recv()
c1.send("Thanks you! I'm good.")
p1 = Process(target=write_pipe, args=[c1])
p2 = Process(target=read_pipe, args=[c2])
p1.start()
time.sleep(1)
p2.start()
p1.join()
p2.join()
from multiprocessing import Process, Queue
def f(q):
q.put([42, None, 'hello'])
if __name__ == '__main__':
q = Queue()
p = Process(target=f, args=(q,))
p.start()
print q.get() # prints "[42, None, 'hello']"
p.join()
from multiprocessing import Process, Value, Array
def f(n, a):
n.value = 3.1415927
for i in range(len(a)):
a[i] = -a[i]
if __name__ == '__main__':
num = Value('d', 0.0)
arr = Array('i', range(10))
p = Process(target=f, args=(num, arr))
p.start()
p.join()
print num.value
print arr
Data structures supported
Values
Array
Python data structures wrappers
Thread/Process safe
@hitul007
http://hitul.in