Python實現的生產者 消費者問題完整例項

2022-09-28 09:42:05 字數 3575 閱讀 2726

生產者、消費者問題,經典的執行緒同步問題:假設有乙個緩衝池(列表),生產者往裡面放東西,消費者從裡面取,規則是:列表為空的時候,生產者才能放東西;列表不為空的時候,消費者才能取東西;為了簡單起見,暫定緩衝池中最多只能有乙個產品。這裡生產者和消費者共同操作乙個資源:緩衝池,因此每次操作的時候,需要給資源加鎖,操作結束時,釋放鎖,這樣才能做到資源同步。使用python實現,需要繼承thread類,獲取鎖物件,**如下:

# -*- coding:utf-8 -*-

#! python2

from threading import thread

from threading import lock

import time,random

pro_list =

lock = lock()

class producer(thread):

def run(self):

global pro_list

while true:

i = random.randint(0, 100)

lock.acquire()

if len(pro_list) > 0:

print "!--product still in list, wait consumer to get it.."

else:

pro_list.append(i)

print ":::producer put:", pro_list[0]

lock.release()

time.sleep(2)

class consumer(thread):

def run(self):

global pro_list

while true:

lock.acquire()

if len(pro_list) == 0:

print "!--no product now, wait producer put in..."

else:

print ":::consumer fetch:", pro_list[0]

pro_list.pop(0)

lock.release()

time.sleep(2)

producer().start()

producer().start()

consumer().start()

producer().start()

producer().start()

consumer().start()

consumer().start()

這裡使用多個生產者和消費者,共同操作緩衝池,部分執行結果如下:

:::producer put: 78

!--product still in list, wait consumer to get it..

:::consumer fetch: 78

:::producer put: 99

!--product still in list, wait consumer to get it..

:::consumer fetch: 99

!--no product now, wait producer put in...

:::producer put: 12

:::consumer fetch: 12

:::producer put: 91

!--product still in list, wait consumer to get it..

!--product still in list, wait consumer to get it..

:::consumer fetch: 91

!--no product now, wait producer put in...

:::producer put: 63

:::consumer fetch: 63

:::producer put: 85

!--product still in list, wait consumer to get it..

!--product still in程式設計客棧 list, wait consumer to get it..

:::consumer fetch: 85

!--no product now, wait producer put in...

:::producer put: 1

:::consumer fetch: 1

:::producer put: 26

!--product still in list, wait consumer to get it..

!--product still in list, wait consumer to get it..

:::consumer fetch: 26

!--no product now, wait producer put in...

:yqxfaapr::producer put: 8

:::consumer fetch: 8

:::producer put: 19

!--product sti程式設計客棧ll in list, wait consumer to get it..

!--product still in list, wait consumer to get it..

:::consumer fetch: 19

!--no product now, wait producer put in...

:::producer put: 74

!--product still in list, wait consumer to get it..

:::consumer fetch: 74

:::producer put: 50

!--product still in list, wait consumer to get it..

:::consumer fetch: 50

!--no product now, wait producer put in...

:::producer put: 97

:::consumer fetch: 97

:::producer put: 69

!--product still in list, wait consumer to get it..

!--product still in list, wait consumer to get it..

:::consumer fetch: 69

!--no product now, wait producer put in...

:::producer put: 41

!--product still in list, wait consumer to get it..

:::consumer fetch: 41

:::producer put: 6

!--product still in list, wait consumer to get it..

:::consumer fetch: 6

!--no product now, wait producer put in...

python 生產者 消費者

from bs4 import beautifulsoup import requests import time import multiprocessing as mp import re from multiprocessing import queue from multiprocessin...

生產者消費者 生產者與消費者模式

一 什麼是生產者與消費者模式 其實生產者與消費者模式就是乙個多執行緒併發協作的模式,在這個模式中呢,一部分執行緒被用於去生產資料,另一部分執行緒去處理資料,於是便有了形象的生產者與消費者了。而為了更好的優化生產者與消費者的關係,便設立乙個緩衝區,也就相當於乙個資料倉儲,當生產者生產資料時鎖住倉庫,不...

生產者消費者

using system using system.collections.generic using system.threading namespace gmservice foreach thread thread in producers q.exit console.read public...