Multithreading Python3多线程,线程返回不并行运行
我正在尝试运行以下线程代码Multithreading Python3多线程,线程返回不并行运行,multithreading,python-3.x,Multithreading,Python 3.x,我正在尝试运行以下线程代码 import Queue import threading def get_items(url): print(url) #Do some stuff with print here return url.split() q = Queue.Queue() urls = [LIST OF URLS] for u in urls[:10]: t = threading.Thread(target=get_items, args=(q
import Queue
import threading
def get_items(url):
print(url)
#Do some stuff with print here
return url.split()
q = Queue.Queue()
urls = [LIST OF URLS]
for u in urls[:10]:
t = threading.Thread(target=get_items, args=(q, u))
t.daemon = True
t.start()
t.join()
这应该并行运行线程,但它是按顺序运行的。其次,如何将线程的return语句的值附加到列表中。当我对代码进行以下更改时,代码成功运行
import Queue
import threading
def get_items(q, url):
print(url)
#Do some stuff with print here
q.put(url.split())
q = Queue.Queue()
urls = [LIST OF URLS]
for u in urls[:10]:
t = threading.Thread(target=get_items, args=(q, u))
t.daemon = True
t.start()
th.append(t)
for t in th:
t.join()
print(q.get())