1

I have read few articles on threading python threads and about GIL.

I wrote a small program to understand some of the concepts

import threading
import time 

def merge_array1(a1):
for x in a1:
  print (x)
  time.sleep(5)

def merge_array2(a1):
  for x in a1:
print (x)


def run():
  threading.Thread(merge_array1([5, 6, 7])).start()
  threading.Thread(merge_array2([8,9,10])).start()


run()

Above program prints something 5,6,7,8,9,10 in sequence

But...i thought when one thread is sleeping, system will automatically run another thread

So output will be something similar to 5,8,9,10,6,7.

Am i missing something here, how to modify this program to give the output expected by me.

Glen D souza
  • 33
  • 1
  • 4
  • You aren't performing your function calls in threads - you are passing the results of those calls (which are `None`) to `Thread()`, so all of the output has already been generated before you go to the next line. – jasonharper May 16 '18 at 11:50

0 Answers0