My CPU has 4 cores so as I understand threads in Python, this means I can only run 4 threads concurrently. I wanted to test this out so I wrote:
import threading
from time import sleep
def print_after_sleep(msg):
sleep(5)
print(msg)
one = threading.Thread(target=print_after_sleep, args=[1])
two = threading.Thread(target=print_after_sleep, args=[2])
three = threading.Thread(target=print_after_sleep, args=[3])
four = threading.Thread(target=print_after_sleep, args=[4])
five = threading.Thread(target=print_after_sleep, args=[5])
six = threading.Thread(target=print_after_sleep, args=[6])
one.start()
sleep(0.2)
two.start()
sleep(0.2)
three.start()
sleep(0.2)
four.start()
sleep(0.2)
five.start()
sleep(0.2)
six.start()
I would expect threads 1-4 to print their messages in rapid succession, but then to wait nearly 5 seconds for threads 5-6 to print their messages. What I actually got, was all threads printing their messages in rapid succession.
