python 實現簡單的生產者消費者模式

2021-10-24 16:17:43 字數 1368 閱讀 6360

今日寫了乙個簡單的生產者消費者指令碼,僅僅是簡單的**;後續如果可以的話以多執行緒的方式,實現快速生產或者快速消耗等擴充套件操作。環境為python27,執行在windows上。

`#-- encoding:utf-8 --

import threading,queue

import subprocess

workqueue = queue.queue()

def product():

for data in range(10):

ip = 「192.168.0.」 + str(data)

res = subprocess.call(「ping -n 2 -w 4 %s」 %ip,stdout=subprocess.pipe)

if res==0:

print(「ping {} success」.format(ip))

workqueue.put(ip)

else:

print(「ping {} failed」.format(ip))

workqueue.put(none)

def consumer():

while true:

data = workqueue.get()

if not data:

print(「data is none!」)

break

else:

print(「consumer data={}」.format(data))

class product(object):

「」「oop product」""

definit(self):

print(「init」)

def ping_o(self,data):

res = subprocess.call("ping -n 2 -w 4 %s" %data)

if 0 == res:

return data

else:

print("ping {} failed".format(data))

ifname== 「main」:

# p = threading.thread(target=product)

# c = threading.thread(target=consumer)

# p.start()

# c.start()

p = product()

msg = input("input ip data:")

res = p.ping_o(msg)

print(res)`

NSCondition 多執行緒解決生產者消費者問題

import viewcontroller.h inte ce viewcontroller 資料緩衝區 property strong,nonatomic nsmutablearray products 執行緒鎖 property strong,nonatomic nscondition cond...

生產者Confirm訊息與Return訊息機制

訊息的確認,是指生產者投遞訊息後,如果broker收到訊息,則會給我們生產者乙個應答 生產者接收應答,用來確定這條訊息是否正常傳送到broker,這種方式也是訊息的可靠性傳遞的核心保障 channel開啟確認模式 channel.confirmselect 在channel上加監聽 addconfi...

python 實現生產者消費者模型

生產者消費者 包子鋪不停的做包子,行人不停的買 這樣就達到了目的 包子的銷售 兩個不同的角色 包子鋪,行人 只負責單一操作 讓包子變成連線的介質.1 coding utf 8 2from threading import thread 3from queue import queue 4import...