Why the output is coming differently on idle and online editor , for the given threading example. Please help in understanding the actual issue which is causing this?
import threading
from time import sleep, time, ctime
loops = [4, 2]
def loop(nloop, nsec):
print('start loop', nloop, 'at:', ctime(time()))
sleep(nsec)
print('loop', nloop, 'done at:', ctime(time()))
def main():
print('starting threads…')
threads = []
nloops = range(len(loops))
for i in nloops:
t = threading.Thread(target=loop,
args=(i, loops[i]))
threads.append(t)
for i in nloops: # start threads
threads[i].start()
for i in nloops: # wait for all
threads[i].join() # threads to finish)
print('all DONE at:', ctime(time()))
if __name__ == '__main__':
main()
Expected output
starting threads…
start loop 0 at: Fri Sep 17 12:43:26 2021
start loop 1 at: Fri Sep 17 12:43:26 2021
loop 1 done at: Fri Sep 17 12:43:28 2021
loop 0 done at: Fri Sep 17 12:43:30 2021
all DONE at: Fri Sep 17 12:43:30 2021
Output coming on idle on my local machine
starting threads…
start loopstart loop 01 at:at: Fri Sep 17 18:19:39 2021Fri Sep 17 18:19:39 2021
loop 1 done at: Fri Sep 17 18:19:42 2021
loop 0 done at: Fri Sep 17 18:19:44 2021
all DONE at: Fri Sep 17 18:19:44 2021