4

The code looks like this :

import time
from threading import Thread

def sleeper(i):
  print "thread %d sleeps for 5 seconds" % i
  time.sleep(5)
  print "thread %d woke up" % i

for i in range(10):
  t = Thread(target=sleeper, args=(i,))
  t.start()

Now this code returns the following :

thread 0 sleeps for 5 seconds
thread 1 sleeps for 5 seconds
thread 2 sleeps for 5 seconds
thread 3 sleeps for 5 seconds
thread 4 sleeps for 5 seconds
thread 5 sleeps for 5 seconds
thread 6 sleeps for 5 seconds
thread 7 sleeps for 5 seconds
thread 8 sleeps for 5 seconds
thread 9 sleeps for 5 seconds
thread 1 woke up
thread 0 woke up
thread 3 woke up
thread 2 woke up
thread 5 woke up
thread 9 woke up
thread 8 woke up
thread 7 woke up
thread 6 woke up
thread 4 woke up

How Thread 1 wokeup before thread 0 meanwhile thread 0 was the first to enter ?

4
  • 4
    Welcome to the world of multi-threading where things do not happen in order. Commented Jan 6, 2016 at 11:14
  • That is the essence of threading. They execute simultaneously, but they can't print simultaneously of course. Therefore, the waking appears out of order on different lines, but in actuality, they finish at the same time. Commented Jan 6, 2016 at 11:15
  • 1
    ...or something close to "same time", as the actual concrete scheduling is up to the OS/scheduler/CPU to figure out and plan. Unless you actually have 10 cores, they can't all finish at the exact same time. Commented Jan 6, 2016 at 11:16
  • @deceze Yeah, even the thread starts are separated by microseconds because Python executes line by line. Commented Jan 6, 2016 at 11:17

1 Answer 1

1

The most common Python-Interpreter (CPython) runs on a single Thread, every Thread you create is just virtual and is still executed on a single core - due to it's GIL (https://wiki.python.org/moin/GlobalInterpreterLock). The order in which they are executed doesn't have to be necessarily the order in which you start the thrads, that's the entire point of having threads - the CPython interpreter will decide which part of which thread is to be executed by any given time. Since you can only use one core and threads are just virtual, you'll never be able to execute 2 threads at the exact same time.

Thanks to Vality for the correction.

Sign up to request clarification or add additional context in comments.

2 Comments

I am sorry but this is wrong. Python does use multiple threads, it is just that CPython, the most common implementation uses a GIL (Global interpreter lock) to ensure that only one thread runs in the python interpreter at once. However it is possible for python extensions written in C to release the GIL allowing true multithreading.
Yes, you are correct and I'm going to update my answer accordingly, but CPython is mostly referenced just as Python, therefore I thought it was clear that we were talking about the same interpreter as the OP, since he didn't mention any specific interpreter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.