线程追加到列表中

时间:2018-05-15 14:45:34

标签: python

如何将线程附加到响应列表?

import requests
import threading

def URLOK(url):
    page = requests.get(url, proxies=proxies)
    return url + ":"+ str(page.status_code)+"\n"

urls = ["http://www.google.com","http://www.amazon.com","http://www.amazon.ca","http://www.google.ca"]

response = []
threads  = []
for url in urls:
    threads.append(threading.Thread(target = URLOK, kwargs ={'url':url} ))
    response.append(threads[-1].start())
for t in threads:                                                           
        t.join()
for response in response:
    print(response)

None添加到列表中。

1 个答案:

答案 0 :(得分:-1)

你不能简单地从这样的线程返回。 thread.start方法只启动线程并返回None。可以在此处找到多个解决方案:how to get the return value from a thread in python?