Created for
Thread
class constructor.start()
methodjoin()
method. This blocks the calling thread until the thread whose join() method is called is terminated
tr_obj = threading.Thread(target=None, name=None, args=(), kwargs={}, daemon=None)
target
- function to be run in a threadname
is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal numberargs
is the argument tuple for the target invocationkwargs
is a dictionary of keyword arguments for the target invocationdaemon
- if not None, a daemonic thread will be created.
import threading
import time
def worker(x):
tid = threading.currentThread().name;
print("x = {} in {}".format(x, tid))
time.sleep(2)
# create the tread
tr = threading.Thread(target=worker, args=(42,))
# start the thread:
tr.start()
# wait until thread terminates:
tr.join()
print("Worker did its job!")
import threading
import time
def worker():
global counter
tmp = counter
print("Before:",counter)
counter = tmp + 1
print("After:",counter)
counter = 0
# create some treads to count together:
thread_pool = []
for i in range(10000):
tr = threading.Thread(target=worker)
tr.start()
thread_pool.append(tr)
# wait for tread to finish:
for tr in thread_pool:
tr.join()
print("Workers counted:", counter)
import threading
import time
def worker():
global counter
# lock the critical section:
lock.acquire()
tmp = counter
print("Before:",counter)
counter = tmp + 1
print("After:",counter)
lock.release()
counter = 0
# create a lock
lock = threading.Lock()
# create some treads to count together:
thread_pool = []
for i in range(10000):
tr = threading.Thread(target=worker)
tr.start()
thread_pool.append(tr)
# wait for tread to finish:
for tr in thread_pool:
tr.join()
print("Workers counted:", counter)
Process
class constructor.start()
methodjoin()
method. This blocks the calling process until the process whose join() method is called is terminatedThe multiprocessing package mostly replicates the API of the threading module
pr_obj = multiprocessing.Process(target=None, name=None, args=(), kwargs={}, daemon=None)
if __name__ == '__main__':
when using processes!
import multiprocessing as mp
import time
def worker(x):
pid = mp.current_process().name;
print("x = {} in {}".format(x, pid))
time.sleep(2)
if __name__ == '__main__':
# create the process
p = mp.Process(target=worker, args=(42,))
# start the process:
p.start()
# wait until process completes:
p.join()
print("Worker did its job as separate Process!")
Value
and Array
classes for Shared memory communication
class multiprocessing.Queue([maxsize])
from multiprocessing import Pool
import time
def worker(n):
# for light work, the pool is not efficient, try with n**10
return n**1000
if __name__ == '__main__':
t =time.time()
# create the Pool:
p = Pool(processes=5)
result = p.map(worker, range(100000))
p.close()
p.join()
print("Pool took: ", time.time() - t)
# serial processing:
t = time.time()
result = []
for x in range(100000):
result.append(worker(x))
# print("Result: ", result)
print("Serial processing took: ", time.time() - t)
These slides are based on
customised version of
framework