码迷,mamicode.com
首页 > 编程语言 > 详细

线程GIL锁 线程队列 回调函数

时间:2018-12-03 16:33:04      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:主线程   multi   进程   process   exception   先进后出   心情   左右   BMI   

----------------------------------无法改变风向,可以调整风帆;无法左右天气,可以调整心情。如果事情无法改变,那就去改变观念。

# # ------------------------------------------------------------------------------------------------------------#


# # --------------[线程队列]--------------

# import queue

#先进先出队列
# q = queue.Queue(3) #创建了一个长度为3的队列,也就是说,最多只能放3个数据
#
# q.put(2)
# print(‘>>>>‘,q.qsize())
# q.put(5)
# print(‘>>>>‘,q.qsize())
# q.put(0)
#
# # q.put(1)
# try:
# q.put_nowait(1)
# except Exception:
# print(‘队列满了‘)
# print(q.get())
# print(q.get())
# print(q.get())
# # print(q.get())
# # print(q.get_nowait())

#后进先出,先进后出

# import queue
#
# q = queue.LifoQueue(3)
#
# q.put(1)
# q.put(2)
# q.put(3)
#
# print(q.get())
# print(q.get())
# print(q.get())

# 优先级队列

import queue

q = queue.PriorityQueue(5)

q.put((3,33))

q.put((1,2))
q.put((1,3))
# q.put((1,(3,2)))
# q.put((1,(1,2)))

q.put((2,22))
q.put((4,44))


print(q.get())
print(q.get())
print(q.get())
print(q.get())
print(q.get())



# # --------------[创建线程池]--------------

import time
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
from multiprocessing import Pool

def func(n):
# print(n)
time.sleep(1)
return n * n

if __name__ == ‘__main__‘:
# t_p = ThreadPoolExecutor(max_workers=4)
t_p = ProcessPoolExecutor(max_workers=4)
# p_pool = Pool(4)
res_list = []

for i in range(10):
res = t_p.submit(func,i) #异步提交任务
# print(res.result()) #等待任务的执行结果,拿不到就阻塞,拿到了再运行
res_list.append(res)

t_p.shutdown()

print(‘主线程结束‘)

for res1 in res_list:
print(res1.result())

# for i in range(10):
# res = p_pool.apply_async(func,args=(i,))
# # print(res)
# print(res.get())

# # --------------[进程池的map方法]--------------

import time
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
from multiprocessing import Pool

def func(n):
# print(n)
time.sleep(1)
return n * n

if __name__ == ‘__main__‘:
t_p = ThreadPoolExecutor(max_workers=4)
# t_p = ProcessPoolExecutor(max_workers=4)
# p_pool = Pool(4)
# res_list = []

res = t_p.map(func,range(10)) #
# print(res)
t_p.shutdown()

print(‘主线程结束‘)
for i in res:
print(i)

# print([t1 for t1 in res])
# for i in range(10):
# res = t_p.submit(func,i) #异步提交任务
# # print(res.result()) #等待任务的执行结果,拿不到就阻塞,拿到了再运行
# res_list.append(res)
#
# t_p.shutdown()
#
# print(‘主线程结束‘)
#
# for res1 in res_list:
# print(res1.result())

# for i in range(10):
# res = p_pool.apply_async(func,args=(i,))
# # print(res)
# print(res.get())

# # --------------[回调函数]--------------


import time
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
from multiprocessing import Pool

def func(n):
time.sleep(1)
return n * n

def call_back(m):
print(‘>>>>>‘,m)
print(m.result())

if __name__ == ‘__main__‘:
# t_p = ThreadPoolExecutor(max_workers=4)
t_p = ProcessPoolExecutor(max_workers=4)
p_pool = Pool(4)
res_list = []

res = t_p.submit(func,10).add_done_callback(call_back)

# p_pool.apply_async(func,args=(10,),callback=call_back)
# for i in range(10):
# res = p_pool.apply_async(func,args=(i,))
# # print(res)
# print(res.get())


线程GIL锁 线程队列 回调函数

标签:主线程   multi   进程   process   exception   先进后出   心情   左右   BMI   

原文地址:https://www.cnblogs.com/dealdwong2018/p/10058978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!