lock = threading.Lock()
def is_even(value):
with lock:
if value % 2 == 0:
print("True")
print('value = {} : result = {}'.format(value,value % 2))
return True
else:
print("False")
print('value = {} : result = {}'.format(value, value % 2))
return False
results = True
def get_result(request, result):
global results
results = result
# data 设置为长度为10的列表,(列表中每一个数作为参数传递给工作函数运行一次)
data = range(10)
# 声明可容纳五个线程的池
pool = threadpool.ThreadPool(5)
# 创建线程运行内容请求列表(线程工作函数,线程工作参数列表,回调函数)
requests = threadpool.makeRequests(is_even, data, get_result)
# 将每一个线程请求扔进线程池
list = []
for req in requests:
if (results):
list.append(pool.putRequest(req))
pool.wait()
else:
break
python多线程获取返回值
最新推荐文章于 2024-06-21 15:10:58 发布