亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

python消息隊列Queue

实例1:消息队列Queue,不要将文件命名为“queue.py”,否则会报异常“ImportError: cannot import name 'Queue'”

1234567891011121314151617181920212223242526272829#coding=utf-8from multiprocessing import Queue  = Queue(3)#初始化一个Queue对象,最多可接收三条put消息q.put('message-1')q.put('message-2')print(q.full())#False,是否满了q.put('message-3')print(q.full())#True #因为消息队列已满,下面的try都会抛出异常,第一个try会等待2秒后再抛出异常,第二个try会立即抛出异常try:    q.put('message-4',True,2)except:    print('except1,消息队列已满,现有消息数量:%s'%q.qsize()) try:    q.put_nowait('message-4')except:    print('except2,消息队列已满,现有消息数量:%s'%q.qsize()) #判断队列是否已满if not q.full():    q.put_nowait('message-4') #读取消息时,先判断消息队列是否为空,在读取if not q.empty():    for in range(q.qsize()):        print(q.get())#q.get会阻塞,q.get_nowait()不阻塞,但会抛异常                   

False

True

except1,消息队列已满,现有消息数量:3

except2,消息队列已满,现有消息数量:3

message-1

message-2

message-3


实例二:通过Process进程间通信

123456789101112131415161718192021222324252627282930313233from multiprocessing import Process,Queueimport os,time,random  #写数据def write(q):    for value in ['A','B','C']:        print('Put %s to queue...'%value)        q.put(value)        time.sleep(random.random()) #读数据def read(q):    while True:        if not q.empty():            value = q.get(True)            print('Get %s from queue...'%value)            time.sleep(random.random())        else:            break if __name__ == '__main__':    print('start...')    = Queue()    #父进程的queue传递给子进程    pw = Process(target=write,args=(q,))    pr = Process(target=read,args=(q,))                   #写进程    pw.start()    pw.join()    #读进程    pr.start()    pr.join()    print('done...')

start...

Put A to queue...

Put B to queue...

Put C to queue...

Get A from queue...

Get B from queue...

Get C from queue...

done...


实例三:通过Manager进程间通信

123456789101112131415161718192021222324252627from multiprocessing import Manager,Poolimport os,time,random  #写数据def writer(q):    print('writer启动(%s),父进程为(%s)'%(os.getpid(),os.getppid()))    for in 'chaoge':        q.put(i) #读数据def reader(q):    print('reader启动(%s),父进程为(%s)'%(os.getpid(),os.getppid()))    for in range(q.qsize()):        print('reader 从Queue获取到消息:%s'%q.get())  if __name__ == '__main__':    print('(%s) start'%os.getpid())    = Manager().Queue()#使用Manager中的Queue来初始化    po=Pool()    #使用阻塞模式创建进程,这样就不需要再reader中使用死循环了,可以等write执行完成后,再用reader    po.apply(writer,(q,))    po.apply(reader,(q,))    #写进程    po.close()    po.join()    print('(%s) End'%os.getpid())

(7720) start

writer启动(7284),父进程为(7720)

reader启动(8712),父进程为(7720)

reader 从Queue获取到消息:c

reader 从Queue获取到消息:h

reader 从Queue获取到消息:a

reader 从Queue获取到消息:o

reader 从Queue获取到消息:g

reader 从Queue获取到消息:e

(7720) End


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消