?print-pdf
' Created for
lscpu | head -10
import psutil
# Get the number of physical cores
physical_cores = psutil.cpu_count(logical=False)
print(f"Number of physical cores: {physical_cores}")
# Get the number of logical processors (hardware threads)
logical_processors = psutil.cpu_count(logical=True)
print(f"Number of logical processors (hardware threads): {logical_processors}")
import multiprocessing
import time
def worker(num):
print(f'Worker {num} starting')
# Simulate some work
time.sleep(2)
print(f'Worker {num} done')
if __name__ == '__main__':
start = time.time()
processes = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
processes.append(p)
p.start()
for p in processes:
p.join()
end = time.time()
print(f'time: {end-start}')
import threading
import time
def worker(num):
print(f'Worker {num} starting')
# Simulate some work
time.sleep(2)
print(f'Worker {num} done')
if __name__ == '__main__':
start = time.time()
threads = []
for i in range(5):
t = threading.Thread(target=worker, args=(i,))
threads.append(t)
t.start()
for t in threads:
t.join()
end = time.time()
print(f'time: {end-start}')
# view all processes:
top
# view all threads per process:
top -H -p <pid>
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(f"Work started in thread {tid}")
time.sleep(5)
print(f"Work ended in thread {tid}")
# 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(x):
tid = threading.currentThread().name
# do some hard and time consuming work:
time.sleep(2)
print(f"Worker {tid} is working with {x}")
#################################################
# Sequential Processing:
#################################################
t = time.time()
worker(42)
worker(84)
print("Sequential Processing took:",time.time() - t,"\n")
#################################################
# Multithreaded Processing:
#################################################
tmulti = time.time()
tr1 = threading.Thread(target=worker, args=(42,))
tr2 = threading.Thread(target=worker, args=(82,))
tr1.start();tr2.start()
tr1.join(); tr2.join()
print("Multithreaded Processing took:",time.time() - tmulti)
You can enjoy the speed of multithreading in Python, if the threaded workers are not CPU intensive.
import threading
import time
max_range = 10_000_000
max_range_half = max_range//2
def worker(r):
tid = threading.currentThread().name
# do some hard and time consuming work:
global result
res = 0
for i in r:
res += i
result += res
print("Worker {tid} is working with {r}")
#################################################
# Sequential Processing:
#################################################
t = time.time()
result = 0
worker(range(max_range_half))
worker(range(max_range_half, max_range))
print("Sequential Processing result: ", result)
print("Sequential Processing took:",time.time() - t,"\n")
#################################################
# Multithreaded Processing:
#################################################
t = time.time()
result = 0
tr1 = threading.Thread(target=worker, args=(range(max_range_half),))
tr2 = threading.Thread(target=worker, args=(range(max_range_half,max_range),))
tr1.start();tr2.start()
tr1.join(); tr2.join()
print("Multithreaded Processing result: ", result)
print("Multithreaded Processing took:",time.time() - t,"\n")