Python: 如何合并 2 个函数的返回值并使用线程将它们附加到列表中?
Python: How combine returned value from 2 functions and appending them into a list using thread?
我是线程的新手,我在使用线程打印列表中的值以允许 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']?
import threading
threading_list = []
def func1():
threading_list.append("HTML")
def func2():
threading_list.append("IS FUN")
thread1 = threading.Thread(target=func1)
thread2 = threading.Thread(target=func2)
x = thread1
y = thread2
x.start()
y.start()
print(threading_list)
在您的 threadlist
中,您正在保存线程 变量 ,因此您在输出中看到的是它们作为字符串的表示形式。
您不能像那样在不同的线程中获取函数 运行 的 return 值。
做你能做的:
- 使用
multithreading
模块:
:
from multiprocessing.pool import ThreadPool
def func1():
return 'HTML'
def func2():
return 'IS FUN'
pool = ThreadPool(processes=1)
return_values = []
return_values.append(pool.apply(func1, ())) # Using apply for synchronous call directly returns the function return value.
func2_result = pool.applyasync(func2) # Using applyasync for asynchronous call will require a later call.
return_values.append(func2_result.get()) # get return value from asynchronous call to func2.
print(return_values)
- 使用可变对象(如列表)来保存 return 值:
:
return_values = []
def func1():
return_values.append('HTML')
def func2():
return_values.append('IS FUN')
# rest of your code here
print(return_values)
你会得到:
['HTML', 'IS FUN']
我是线程的新手,我在使用线程打印列表中的值以允许 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']?
import threading
threading_list = []
def func1():
threading_list.append("HTML")
def func2():
threading_list.append("IS FUN")
thread1 = threading.Thread(target=func1)
thread2 = threading.Thread(target=func2)
x = thread1
y = thread2
x.start()
y.start()
print(threading_list)
在您的 threadlist
中,您正在保存线程 变量 ,因此您在输出中看到的是它们作为字符串的表示形式。
您不能像那样在不同的线程中获取函数 运行 的 return 值。 做你能做的:
- 使用
multithreading
模块:
:
from multiprocessing.pool import ThreadPool
def func1():
return 'HTML'
def func2():
return 'IS FUN'
pool = ThreadPool(processes=1)
return_values = []
return_values.append(pool.apply(func1, ())) # Using apply for synchronous call directly returns the function return value.
func2_result = pool.applyasync(func2) # Using applyasync for asynchronous call will require a later call.
return_values.append(func2_result.get()) # get return value from asynchronous call to func2.
print(return_values)
- 使用可变对象(如列表)来保存 return 值:
:
return_values = []
def func1():
return_values.append('HTML')
def func2():
return_values.append('IS FUN')
# rest of your code here
print(return_values)
你会得到:
['HTML', 'IS FUN']