I have the below code:
from threading import Thread
if __name__ == '__main__':
t1 = Thread(target=fun1, args=())
t2 = Thread(target=fun2, args=())
t3 = Thread(target=fun3, args=())
t4 = Thread(target=fun4, args=())
start = time.clock()
end = time.clock()
print("Time Taken = ",end-start)
Q2: If the threads each did something that took 5 seconds of processing time, then you would expect the total time to be 20 seconds. But each thread is just sleeping for 5 seconds, so each thread releases the
GIL, and thus allows other threads to run "in parallel" (only conceptually), as it waits for the sleep timeout.
Q3: Multiprocessing, unlike
threads, creates child processes which can each run on different processors concurrently (actually parallel). But even if these
sleeps each run on separate processors, they will still collectively finish in about 5 seconds. If they run on the same processor the OS's time-sharing mechanism will, in a manner similar to Python's threading mechanism, also ensure they complete in about 5 minutes.
Q4: It's the same concept as with
ping is not CPU-intensive and thus its thread rarely has possession of the GIL. This allows all three
ping threads to conceptually run in parallel.