Junior — Middle
What is your practical experience with multithreading and thread management in applications?
sobes.tech AI
Answer from AI
In my practical experience with multithreading in Python, I have used the threading and concurrent.futures modules for parallel execution of tasks that do not heavily load the CPU, such as network requests or input/output operations.
I am also familiar with the Global Interpreter Lock (GIL), which limits the concurrent execution of Python bytecode in multiple threads. Therefore, for CPU-intensive tasks, I prefer to use multiprocessing via the multiprocessing module.
An example of using threading to run multiple threads:
import threading
import time
def worker(num):
print(f'Thread {num} started')
time.sleep(2)
print(f'Thread {num} finished')
threads = []
for i in range(3):
t = threading.Thread(target=worker, args=(i,))
threads.append(t)
t.start()
for t in threads:
t.join()
Thus, I know how to create and manage threads, synchronize their work, and consider Python's limitations when designing multithreaded applications.