Home > other >  Execute threads in certain order
Execute threads in certain order

Time:10-15

When we launch threads, is it known for SURE which thread will be executed first or is it something not predictable ?

I say this because square is always called first and then cube.

import threading


def print_cube(num):
    # function to print cube of given num

    print("Cube: {}".format(num * num * num))


def print_square(num):
    # function to print square of given num
    print("Square: {}".format(num * num))


if __name__ == "__main__":
    # creating thread
    cuadrado = threading.Thread(target=print_square, args=(10,))
    cubo = threading.Thread(target=print_cube, args=(10,))

    # starting thread 1
    cuadrado.start()
    # starting thread 2
    cubo.start()

    print("Done!")

I would like to understand the method Threading.start()

Does the order of calling Threading start() matter?

But, if I sleep the yarns with the same time, then it is random order

import threading
import time


def print_cube(num):
    # function to print cube of given num

    time.sleep(3)
    print("Cube: {}".format(num * num * num))


def print_square(num):
    # function to print square of given num
    time.sleep(3)
    print("Square: {}".format(num * num))


if __name__ == "__main__":
    # creating thread
    cuadrado = threading.Thread(target=print_square, args=(10,))
    cubo = threading.Thread(target=print_cube, args=(10,))

    # starting thread 1
    cuadrado.start()
    # starting thread 2
    cubo.start()


    # both threads completely executed
    print("Done!")

CodePudding user response:

I would like to understand the method Threading.start()

A Python threading.Thread object is not the same thing as a thread. A thread is an object in the operating system—separate from your code. I like to think of a thread as an agent who executes your target function.

The purpose of the Python Thread class is, to provide a platform-independent interface to the various different thread APIs of various different operating systems. One peculiarity of Python's Thread is that it does not actually create the operating system thread until you call its start() method. That's what start() does: It creates the underlying OS thread.


Does the order of calling Threading start() matter?

Depends what you mean. Your program definitely always starts the cuadrado thread before it starts the cubo thread, but the whole point of threads is to provide a means to achieve concurrency in your program; and what "concurrency" means is that the things happening in different threads are not required to happen in any definite order. By calling print_cube() and print_square() in different threads, you effectively are telling Python (and the OS) that you don't care which one prints first.

Maybe print_square() will always be called first on your computer. Maybe print_cube() will always be called first on somebody else's computer. Maybe it will be unpredictable which one goes first on a third computer.

Sounds a little chaotic, but the reason why we like concurrency is that it gives the OS and the Python system more freedom to get things done in the most efficient order. E.g., if one thread is waiting for some network packet to arrive, some other thread can be allowed to do some useful work. So long as the "useful work" doesn't need the packet that the other thread was waiting for, that's a Good Thing.

  • Related