[英]Confusion regarding the output of threads in python
I am currently working with python v.2.7 on windows 8. 我目前正在Windows 8上使用python v.2.7。
My programme is using threads. 我的程序正在使用线程。 I am providing a name to these threads during their creation.
我在创建这些线程时为其命名。 The first thread is named
First-Thread
and second one is named Second-Thread
. 第一个线程名为
First-Thread
,第二个Second-Thread
名为Second-Thread
。 The threads execute a method named as getData()
that does the following: 线程执行名为
getData()
的方法,该方法执行以下操作:
compareValues()
compareValues()
compareValues
() and adds them to a list called myList
compareValues
()中检索信息,并将其添加到名为myList
的列表中 The compareValues()
does the following: compareValues()
执行以下操作:
I save the results of these threads to a list named as myList
and then finally print this myList
. 我将这些线程的结果保存到名为
myList
的列表中,然后最终打印此myList
。
Problem : Why I never see the Second-Thread
in myList
? 问题 :为什么我从未在
myList
看到Second-Thread
? I don't understand this behavior. 我不了解这种行为。 Please try to execute this code to see the output in order to understand my problem.
请尝试执行此代码以查看输出,以了解我的问题。
Code : 代码 :
import time
from random import randrange
import threading
myList = []
def getData(i):
print "Sleep for %d"%i
time.sleep(i)
data = compareValues()
for d in list(data):
myList.append(d)
def compareValues():
number = randrange(10)
if number >= 5:
yield "%s: Greater than or equal to 5: %d "%(t.name, number)
else:
yield "%s: Less than 5: %d "%(t.name, number)
threadList = []
wait = randrange(10)+1
t = threading.Thread(name = 'First-Thread', target = getData, args=(wait,))
threadList.append(t)
t.start()
wait = randrange(3)+1
t = threading.Thread(name = 'Second-Thread', target = getData, args=(wait,))
threadList.append(t)
t.start()
for t in threadList:
t.join()
print
print "The final list"
print myList
Sample output : 样本输出 :
Sleep for 4Sleep for 1
The final list
['First-Thread: Greater than or equal to 5: 7 ', 'First-Thread: Greater than or equal to 5: 8 ']
Thank you for your time. 感谢您的时间。
def compareValues():
number = randrange(10)
if number >= 5:
yield "%s: Greater than or equal to 5: %d "%(t.name, number)
else:
yield "%s: Less than 5: %d "%(t.name, number)
In the body of compareValues
the code refers to t.name
. 在
compareValues
主体中,代码引用t.name
。 By the time compareValues()
gets called by the threads, t
, which is looked up according to the LEGB rule and found in the global scope , references the first thread because the t.join()
is waiting on the first thread. 到线程被
compareValues()
调用时,根据LEGB规则查找并在全局范围内找到的t
引用了第一个线程,因为t.join()
正在等待第一个线程。 t.name
thus has the value First-Thread
. 因此
t.name
的值为First-Thread
。
To get the current thread name, use threading.current_thread().name
: 要获取当前线程名称,请使用
threading.current_thread().name
:
def compareValues():
number = randrange(10)
name = threading.current_thread().name
if number >= 5:
yield "%s: Greater than or equal to 5: %d "%(name, number)
else:
yield "%s: Less than 5: %d "%(name, number)
Then you will get output like 然后你会得到像
Sleep for 4
Sleep for 2
The final list
['Second-Thread: Less than 5: 3 ', 'First-Thread: Greater than or equal to 5: 5 ']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.