4

I'm learning python 3 and I watched this tutorial about threading:

https://www.youtube.com/watch?v=WaXK8G1hb_Q&index=33&list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_#t=365.299172

I tested the example code but for some reason the result is sort of weird.

import threading

class Messenger(threading.Thread):
    def run(self):
        for _ in range(10):
            print(threading.currentThread().getName())


m1 = Messenger(name="Send Messages")
m2 = Messenger(name="Receive Messages")
m1.start()
m2.start()

I was expecting the program to print out "Send Messages" and "Receive Messages" on a sort of random order, but this what happened, and I'm not quite sure why:

Send MessagesReceive Messages

Send MessagesReceive Messages

Send MessagesReceive Messages

Send MessagesReceive Messages

Send MessagesReceive Messages

Send MessagesReceive Messages

Send MessagesReceive Messages

Send MessagesReceive Messages

Send MessagesReceive Messages

Send MessagesReceive Messages

Can somebody explain to me why were the results printed this way? Thanks for any help given!

Fargo
  • 67
  • 4
  • Are you absolutely certain you're running Python 3? When I ran your sample code in Python 3.4, I got results like in the answer below, but when I used Python 2.7, I had a few lines like you showed above. Also, see this question: http://stackoverflow.com/q/7687862/3820658. – jb326 May 28 '16 at 19:45

1 Answers1

2

I actually ended up getting different results when I ran your code snippet. The likely situation that is happening is that the CPU is scheduling your threads all in the "correct" order on the run you posted, but will produce different results if you continue to run it multiple times. This is normal threading behavior, because the CPU essentially decides when each process gets to run.

I would also make sure you are running the exact code you posted, because my output was separated by new line characters while yours seems to not be.

Send Messages
Send Messages
Receive Messages
Send Messages
Send Messages
Send Messages
Send Messages
Receive Messages
Receive Messages
Send Messages
Receive Messages
Send Messages
Receive Messages
Send Messages
Receive Messages
Send Messages
Receive Messages
Receive Messages
Receive Messages
Receive Messages

Running this code:

import threading

class Messenger(threading.Thread):
    def run(self):
        for _ in range(10):
            print(threading.currentThread().getName())


m1 = Messenger(name="Send Messages")
m2 = Messenger(name="Receive Messages")
m1.start()
m2.start()

If you would like to read more:

https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/5_CPU_Scheduling.html

The important part is the section on CPU Scheduler:

  • Whenever the CPU becomes idle, it is the job of the CPU Scheduler ( a.k.a. the short-term scheduler ) to select another process from the ready queue to run next.

  • The storage structure for the ready queue and the algorithm used to
    select the next process are not necessarily a FIFO queue. There are
    several alternatives to choose from, as well as numerous adjustable
    parameters for each algorithm, which is the basic subject of this
    entire chapter.

Kush
  • 956
  • 6
  • 14