【发布时间】:2018-04-28 20:08:21
【问题描述】:
我是线程新手,我在使用线程打印列表中的值以允许 2 个函数同时工作并将结果附加到列表时遇到异常结果。在我的代码下面:
import threading
def func1():
return "HTML"
def func2():
return "IS FUN"
threadslist = []
thread1 = threading.Thread(target=func1)
thread2 = threading.Thread(target=func2)
x = thread1
y = thread2
x.start()
y.start()
threadslist.append(x)
threadslist.append(y)
print(threadslist)
这是列表的结果:
[<Thread(Thread-1, stopped 1076)>, <Thread(Thread-2, stopped 7948)>]
为什么它存储 Threads 对象而不是存储 ['HTML', 'IS FUN'] ?
【问题讨论】:
-
线程实际上并没有捕获每个函数的返回值,只是执行它。
-
你可能想使用一个线程池,如下所示:stackoverflow.com/questions/6893968/…
标签: python multithreading concurrency threadpool python-multithreading