简体   繁体   English

关于python中线程输出的困惑

[英]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()的方法,该方法执行以下操作:

  • makes the current thread to sleep for some time 使当前线程休眠一段时间
  • calls the compareValues() 调用compareValues()
  • retrieve the information from the compareValues () and adds them to a list called myList compareValues ()中检索信息,并将其添加到名为myList的列表中

The compareValues() does the following: compareValues()执行以下操作:

  • generates a random number 产生一个随机数
  • checks if it is less than 5 or if it is greater than or equal to 5 and yields the result along with the current thread's name 检查它是否小于5或大于或等于5,并产生结果以及当前线程的名称

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM