From: LinkedIn course ‘Python Essential Training’ by Ryan Mitchell https://www.linkedin.com/learning/python-essential-training-14898805
import threading
import time
Threads
def longSquare(num):
time.sleep(1)
return num**2
[longSquare(n) for n in range(0, 5)]
## [0, 1, 4, 9, 16]
t1 = threading.Thread(target=longSquare, args=(1,)) #args is tuple
t2 = threading.Thread(target=longSquare, args=(2,))
t1.start()
t2.start()
t1.join()
t2.join()
def longSquare(num, results):
time.sleep(1)
results[num] = num**2
results = {}
t1 = threading.Thread(target=longSquare, args=(1, results)) #args are tuples
t2 = threading.Thread(target=longSquare, args=(2, results))
t1.start()
t2.start()
t1.join()
t2.join()
print(results)
## {1: 1, 2: 4}
Laborious to write t1 t2 etc… so we can put them in a list.
def longSquare(num, results):
time.sleep(1)
results[num] = num**2
results = {}
threads = [threading.Thread(target=longSquare, args=(n, results)) for n in range(0,100)] # a hundred threads
[t.start() for t in threads]
## [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
[t.join() for t in threads]
## [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
print(results)
## {2: 4, 4: 16, 1: 1, 7: 49, 5: 25, 6: 36, 3: 9, 10: 100, 9: 81, 0: 0, 8: 64, 13: 169, 14: 196, 11: 121, 19: 361, 42: 1764, 85: 7225, 98: 9604, 97: 9409, 45: 2025, 30: 900, 15: 225, 35: 1225, 43: 1849, 50: 2500, 26: 676, 49: 2401, 34: 1156, 54: 2916, 55: 3025, 64: 4096, 65: 4225, 68: 4624, 69: 4761, 74: 5476, 17: 289, 25: 625, 57: 3249, 21: 441, 62: 3844, 89: 7921, 59: 3481, 91: 8281, 84: 7056, 46: 2116, 95: 9025, 60: 3600, 61: 3721, 70: 4900, 81: 6561, 94: 8836, 31: 961, 56: 3136, 99: 9801, 12: 144, 51: 2601, 27: 729, 73: 5329, 39: 1521, 23: 529, 33: 1089, 37: 1369, 77: 5929, 29: 841, 18: 324, 32: 1024, 20: 400, 83: 6889, 36: 1296, 86: 7396, 53: 2809, 75: 5625, 28: 784, 88: 7744, 71: 5041, 82: 6724, 93: 8649, 96: 9216, 90: 8100, 48: 2304, 41: 1681, 47: 2209, 24: 576, 40: 1600, 58: 3364, 63: 3969, 66: 4356, 80: 6400, 67: 4489, 44: 1936, 22: 484, 79: 6241, 76: 5776, 78: 6084, 92: 8464, 52: 2704, 87: 7569, 72: 5184, 38: 1444, 16: 256}
Multiprocessing. Multiprocessing does not share memory like multi-threading
from multiprocessing import Process
#import myfunction #need to import the function you use unless you use the package below:
#pip install multiprocess
def longSquare(num, results):
time.sleep(1)
print(num**2)
print('Finished computing!')
results = {}
processes = [Process(target=longSquare, args=(n,results)) for n in range(0,10)]
[p.start() for p in processes]
[p.join() for p in processes]