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...