python多线程队列数据丢失_python并发编程之线程剩余内容(线程队列,线程池)及协程...

1. 线程的其他方法

importthreadingimporttimefrom threading importThread,current_threaddeff1(n):

time.sleep(1)print('子线程名称', current_thread().getName()) #获取线程名

print('%s号线程任务'%n)if __name__ == '__main__':

t1= Thread(target=f1,args=(1,))

t1.start()print('主线程名称',current_thread().getName()) #获取线程名

print('主线程ID',current_thread().ident) #获取线程id

print(current_thread()) #当前线程对象

print(threading.enumerate()) #当前正在运行的线程对象的一个列表 [<_mainthread started>, ]

print(threading.active_count()) #当前正在运行的线程数

2. 线程队列

线程队列中三种队列形式,所使用的方法相同,都有put(),get(),put_nowait(),get_nowait(),qsize(),full(),empty() 等这些方法.就只传一组先进先出的代码

import queue

先进先出队列:queue.Queue(n)

q = queue.Queue(3)

q.put(1)

q.put(2)print('当前队列内容长度',q.qsize())

q.put(3)print('查看队列是否满了',q.full())try:

q.put_nowait(4)exceptException:print('队列满了')print(q.get())print(q.get())print(q.get())print('查看队列是否为空',q.empty())try:

q.get_nowait()exceptException:print('队列空了')

先进后出 / 后进先出队列:queue.LifoQueue(n)

优先级队列:

queue.priorityQueue(n)

优先级队列中如果第一个参数相同,后面的比较方法为下面的描述.

如果说值里面的元素是数字类型,那么当两个值的优先级相同时,比较的是两个值的大小,小的优先被取出来.如果元素是字符串,那么依次比较每个字母的ascii表中的位置,小的优

先被取出来.如果put的数据是一个元组,元组的第一个参数是优先级数字,数字越小优先级越高,越先被get到被取出来,第二个参数是put进去的值,如果说优先级相同,那么值别忘了应

该是相同的数据类型,字典不行

3. 线程池

from concurrent_futures import ThreadPoolExecutor,ProcessPoolExecutor

p = ThreadPoolExecutor(4)  #默认的线程个数是cpu个数 * 5

p = ProcessPoolExecutor(4)  #默认的进程个数是cpu个数

map(f1,可迭代对象)  : 异步提交任务

sublim(f1,参数)  : 异步提交任务,和get方法一样,如果没有结果,会等待,阻塞程序

shutdown()  : 锁定线程池,等待线程池中所有已经提交的任务全部执行完毕 , 相当于close + join

importtimefrom threading importcurrent_threadfrom concurrent.futures importThreadPoolExecutor,ProcessPoolExecutordeff1(n,s):

time.sleep(1)#print('%s号子线程'%current_thread().ident)

#print(n,s)

return

if __name__ == '__main__':

tp= ThreadPoolExecutor(4)#tp = ProcessPoolExecutor(4)

#tp.map(f1,range(10)) #异步提交任务,参数同样是任务名称,可迭代对象

res_list =[]for i in range(10):

res= tp.submit(f1,i,'baobao') #submit是给线程池异步提交任务,

print(res)#res.result()

res_list.append(res)for r inres_list:print(r.result())

tp.shutdown()#主线程等待所有提交给线程池的任务,全部执行完毕 close + join

#for r in res_list:

#print(r.result())

print('主线程结束')

线程池回调函数

线程池的回调函数与进程池的相似

importtimefrom concurrent.futures importThreadPoolExecutor,ProcessPoolExecutordeff1(n,s):return n*sdeff2(n):print('回调函数',n.result())if __name__ == '__main__':

tp= ThreadPoolExecutor(4)

res= tp.submit(f1,11,12).add_done_callback(f2)

4. 协程

生成器版

importtimedeff1():for i in range(10):

time.sleep(0.5)print('f1=>',i)yield

deff2():

g=f1()for i in range(10,20):

time.sleep(0.5)print('f2=>', i)

next(g)

f1()

f2()

greenlet模块

importtimefrom greenlet importgreenletdeff1():print('第一次f1')

g2.switch()#切换到g2这个对象的任务去执行

time.sleep(1)print('第二次f1')

g2.switch()deff2():print('第一次f2')

g1.switch()

time.sleep(1)print('第二次f2')

g1= greenlet(f1) #实例化一个greenlet对象,并将任务名称作为参数参进去

g2 =greenlet(f2)

g1.switch()#执行g1对象里面的任务

gevent模块

from gevent import monkey;monkey.patch_all()

这个模块只要有io的地方就会自动切换,不必非要用gevent模块,下面的代码中gevent.sleep(1) 换成time.sleep(1)也可以执行.

importgeventfrom gevent importmonkey;monkey.patch_all()importtimeimportthreadingdeff1():print('第一次f1')

gevent.sleep(1) #time.sleep(1)

print('第二次f1')deff2():print('第一次f2')

gevent.sleep(1) #time.sleep(1)

print('第二次f2')

g1= gevent.spawn(f1) #异步提交了f1任务

g2 = gevent.spawn(f2) #异步提交了f2任务

gevent.joinall([g1,g2]) #相当于g1.join()+g2.join()

print('主程序任务')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值