0

I started learning threads in python.
I saw this code here that says that if i want to run 2 threads at the same time I am learning from a free udemy course and this is how he prints the threads name.
Only he gets "thread-1", "thread-2". I should do it like this:

if __name__ == '__main__':
Thread(target = displayNums()).start()
Thread(target = stam()).start()

Saw it here: Make 2 functions run at the same time

Now, what I am trying to do is to run 2 or 3 threads at the same time, and get their name. But I always get MainThread my code:

from threading import *

def displayNums():
    i = 0
    print(current_thread().getName() , "suo")
    while i <= 10:
        print(i)
        i+=1
print(current_thread().getName())
t = Thread(target=displayNums())


def stam():
    k = 0
    print(current_thread().getName(), "sss")
    while k <= 50:
        print(k,"k")
        k+=1



if __name__ == '__main__':
    Thread(target = displayNums()).start()
    Thread(target = stam()).start()

I get no errors but i get this output:

    MainThread
MainThread suo
0
1
2
3
4
5
6
7
8
9
10
MainThread suo
0
1
2
3
4
5
6
7
8
9
10
MainThread sss
0 k
1 k
2 k
3 k
4 k
5 k
6 k
7 k
8 k
9 k
10 k
11 k
12 k
13 k
14 k
15 k
16 k
17 k
18 k
19 k
20 k
21 k
22 k
23 k
and so on...

2 Answers2

0

Your code is actually calling thread targets outside of threads. Edit your code to:

if __name__ == '__main__':
    Thread(target = displayNums).start()
    Thread(target = stam).start()
Marcin Raczyński
  • 944
  • 1
  • 10
  • 14
0

Change this:

if __name__ == '__main__':
    Thread(target = displayNums()).start()
    Thread(target = stam()).start()

to this:

if __name__ == '__main__':
    Thread(target = displayNums).start()
    Thread(target = stam).start()

The thread target is the function itself, not the returned value from the function. stam() calls the function and returns its value, but stam is the function object itself. When you create a thread you want the function itself, not its returned value.

EDIT: You call a function when you want to run it right now. You can access its returned value, for example x = f() will assign the returned value of f to x. There are other situations where you don't want to run a function right now, but you want to keep track of it so you can call it later. This is the case with threads. You don't want to run the function, you want to tell the thread constructor what function to run in the second thread. So in that case you pass the function itself as an argument. Usually that means the function name without any brackets.

Python treats functions like any other object. In Python you can assign a function to a variable, and then the variable is, for all intents and purposes, the function itself. It can be called just like the original function (with curly braces) or it can be passed to a function, or whatever.

Try this:

# print is a function (built-in), and I can assign it to a variable
a = print
# Nothing gets printed yet
# the variable 'a' is now the same thing as the print function
a("Hello world")

This is a silly example but there are lots of real-world situations where this is very useful.

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
  • Marcin's answer was up before mine, so please approve his answer in preference to mine. I'll leave mine up because my explanation is a little different and that might be helpful. – Paul Cornelius Feb 13 '20 at 00:43
  • thank you both :) it's working. how do i know in which cases i should invoke the method like in your examples or with the brackets? it very confusing for a new learner. plus if you can please explain the `if`. i know its comparing two names, but why the string is `MainThread` for example? – NoobProgrammer Feb 13 '20 at 00:56
  • Thread names are usually just created by python automatically. If you want to assign a particular name to a new thread,you can use the name= keyword in the constructor. I've rarely if ever found a use for that. I am modifying my answer to try to answer your first question.. – Paul Cornelius Feb 13 '20 at 01:44