Python訊息佇列

2021-09-21 04:52:24 字數 3719 閱讀 9354

訊息中介軟體 --->就是訊息佇列

非同步方式:不需要立馬得到結果,需要排隊

同步方式:需要實時獲得資料,堅決不能排隊

例子:#多程序模組multiprocessing

from multiprocessing import process

from multiprocessing import queue

def write(q):

for i in ["a", "b", "c", "d"]:

q.put(i)

print ("put to queue".format(i))

def read(q):

while 1:

result = q.get()

print ("get from queue".format(result))

#寫乙個主函式

def main():

q = queue()

pw = process(target=write, args=(q,))

pr = process(target=read, args=(q,))

pw.start()

pr.start()

pw.join()

#終止pr執行緒

pr.terminate()

if __name__ == '__main__':

#呼叫主函式

main()

輸出:put a to queue

put b to queue

put c to queue

put d to queue

多程序模組multiprocessing中pipe方法實現訊息佇列

例子:

from multiprocessing import pipe, process

import time

def proce1(pipe):

for i in xrange(1, 10):

pipe.send(i)

print ("send to pipe".format(i))

time.sleep(1)

def proce2(pipe):

n = 9

while n > 0 :

result = pipe.recv()

print ("recv from pipe".format(result))

def main():

pipe = pipe(duplex=false)

print (type(pipe))

p1 = process(target=proce1, args=(pipe[1],))

p2 = process(target=proce2, args=(pipe[0],))

p1.start()

p2.start()

p1.join()

p2.join()

pipe[0].close()

pipe[1].close()

if __name__ == '__main__':

main()

輸出:send 1 to pipe

recv 1 from pipe

recv 2 from pipe

send 2 to pipe

recv 3 from pipe

send 3 to pipe

recv 4 from pipe

send 4 to pipe

send 5 to pipe

recv 5 from pipe

recv 6 from pipe

send 6 to pipe

send 7 to pipe

recv 7 from pipe

send 8 to pipe

recv 8 from pipe

send 9 to pipe

recv 9 from pipe

模仿生產者和消費者的多執行緒訊息佇列練習

例子:from threading import thread

from multiprocessing import queue

import time

class proceduer(thread):

def __init__(self, queue):

super(proceduer, self).__init__()

self.queue = queue

def run(self):

try:

for i in xrange(1, 10):

print ("put data is to queue".format(i))

self.queue.put(i)

except exception as e:

print ("put data error")

raise e

class consumer_odd(thread):

def __init__(self, queue):

super(consumer_odd, self).__init__()

self.queue = queue

def run(self):

try:

while not self.queue.empty:

number = self.queue.get()

if number%2 != 0:

print ("get from queue odd. thread name is ".format(number, self.getname()))

else:

self.queue.put(number)

time.sleep(1)

except exception as e:

raise e

class consumer_even(thread):

def __init__(self, queue):

super(consumer_even, self).__init__()

self.queue = queue

def run(self):

try:

while not self.queue.empty:

number = self.queue.get()

if number%2 == 0:

print ("get from queue even.thread name is".format(number, self.getname()))

else:

self.queue.put(number)

time.sleep(1)

except exception as e:

raise e

def main():

queue = queue()

p = proceduer(queue=queue)

p.start()

p.join()

time.sleep(1)

c1 = consumer_odd(queue=queue)

c2 = consumer_even(queue=queue)

c1.start()

c2.start()

c1.join()

c2.join()

print ("all thread terminate")

if __name__ == '__main__':

main()

python 訊息佇列

1.先進先出 2.後進先出 3.優先順序佇列 4.雙向佇列 import queue q queue.queue 2 佇列最大長度 q.put 11 q.put 22 print q.qsize 獲取佇列的個數 print q.get print q.get 先進先出佇列 put放資料,block是...

python 操作訊息佇列

其中p指producer,即生產者 c指consumer,即消費者。中間的紅色表示訊息佇列,例項中表現為hello佇列。往佇列裡插入資料前,檢視訊息佇列 sudo rabbitmqctl list queues listing queues celeryev.db53a5e0 1e6a 4f06 a...

python 操作訊息佇列

閱讀目錄 回到頂部 其中p指producer,即生產者 c指consumer,即消費者。中間的紅色表示訊息佇列,例項中表現為hello佇列。往佇列裡插入資料前,檢視訊息佇列 sudo rabbitmqctl list queues listing queues celeryev.db53a5e0 1...