Conditions lock condition and Queue ()

Before learning you should unlock and queue basis

import queue
import time
import random
import threading
import asyncio
import   logging
# from queue import Empty
logging.basicConfig(level = logging.INFO,format = '%(asctime)s  - %(levelname)s -->%(funcName)s  at line %(lineno)d: \n %(message)s')
log= logging.getLogger()
# queue.qsize 不可以限制,作为应答式condition
q_init = queue.Queue(5)

async def jobs(item):
    time.sleep(random.randint(1,3))
    status = random.randint(0, 1)
    if status == 0:
        return ("success",item)
    else:
        return ("failed",item)

async def do_work(item):
    logging.info("do something %s,time start %s" % (item, time.asctime()))
    a =await jobs(item)
    return a

def async_runner(checker):
    new_loop = asyncio.new_event_loop()
    asyncio.set_event_loop(new_loop)
    loop = asyncio.get_event_loop()
    task = asyncio.ensure_future(do_work(checker))
    loop.run_until_complete(asyncio.wait([task]))
    st = task.result()
    return st

def worker_consumer(q_init,cond):
        # checker=None
        # while checker !="stop":
        while True:
            try:
                if cond.acquire():
                    if not q_init.empty():
                        cond.notify()
                        checker = q_init.get()
                        if checker == "stop":
                            cond.release()
                            BREAK 
                        ST = async_runner (Checker) 
                        IF ST [0] in [ "Success", "failed"]: 
                            logging.info ( "% S% S IS Task Status Finished"% (ST [. 1], ST [0])) 
                            logging.info ( "% s king we ate quail eggs, some more of it" Checker%) 
                            q_init.task_done () 
                    cond.release () 
            the except Exception: 
                 logging.debug("queue is empty ")

def producer(cond,q_init):
        Item 1 = 
        the while True: 
            IF cond.acquire (): 
                IF q_init. QSize () <. 5: 
                    q_init.put (Item) 
                    logging.info ( "baby who added the notice quail eggs can eat them% s"% item)
                    cond.notify()
                else:
                    cond.wait()
                cond.release()
                item += 1
                if item >= 11:
                   break
        q_init.join()
        for  i in range(thread_num):
            q_init.put("stop")

if __name__ == '__main__':
        # 消费者>生产者
        thread_num=5
        cond=threading.Condition()
        producer = [threading.Thread(target=producer,args=(cond,q_init)) for i in range(1)]
        consumer = [threading.Thread(target=worker_consumer, args=(q_init,cond)) for i in range(thread_num)]
        for p in producer:
                p.start()
        for k in consumer:
                k.start()

        for m in consumer:
            m.join()

  result:

2019-12-21 12: 50: 01,781 - INFO -> producer at line 70: 
 children are all, plus a notification under can eat them quail eggs 1 
2019-12-21 12: 50: 01,781 - INFO -> producer at line 70: 
 children are, the notification can be added nor eat quail eggs 2 
2019-12-21 12: 50: 01,781 - INFO -> producer at line 70: 
 children are, plus the 3 nor eat quail eggs notification may 
2019 -12-21 12: 50: 01,781 - INFO -> producer at line 70: 
 children are all, plus the 4 quail eggs notification can eat them 
2019-12-21 12: 50: 01,782 - INFO -> producer at line 70: 
 baby who added the notice 5 can eat them quail eggs 
2019-12-21 12 is: 50: 01,785 - the INFO -> do_work aT Line 22 is: 
 do something. 1, 21 is Time Start Sat On Dec 2019 12:50:01 
12 is 2019-12-21: 50: 04,787 - the INFO -> Line 57 is worker_consumer AT: 
 . 1 Task Status Finished IS failed 
2019-12-21 12 is: 50: 04,787 - the INFO -> worker_consumer AT Line 58:
 We finished 1 king quail eggs, some more of it 
2019-12-21 12: 50: 04,788 - INFO -> do_work AT Line 22: 
 do something 2, Start Time Sat Dec 21 12:50:04 2019 
2019-12- 12 21: 50: 06,789 - INFO -> worker_consumer AT Line 57: 
 2 Task finished IS failed the Status 
2019-12-21 12: 50: 06,789 - INFO -> worker_consumer AT Line 58: 
 king we finished 2 quail eggs, some more of it 
2019-12-21 12: 50: 06,791 - INFO -> do_work AT Line 22: 
 do something 3, Start Time Sat Dec 21 12:50:06 2019 
2019-12-21 12: 50: 08,793 - INFO -> worker_consumer AT Line 57: 
 3 failed the Task finished IS Status 
2019-12-21 12: 50: 08,793 - INFO -> worker_consumer AT Line 58: 
 king we had finished three quail eggs, some more of it 
2019-12-21 12: 50: 08,794 - INFO - > do_work at line 22:
 something. 4 do, Start Time Sat On Dec 21 is 12:50:08 2019 
2019-12-21 12 is: 50: 09,795 - the INFO -> Line 57 is worker_consumer AT: 
 . 4 Task Status Finished IS failed 
2019-12-21 12:50: 09,795 - INFO -> worker_consumer at line 58: 
 king we had finished the 4 quail eggs, some more of it 
2019-12-21 12: 50: 09,796 - INFO -> do_work AT Line 22: 
 do something 5, Start Time Sat Dec 2019 12:50:09 21 is 
2019-12-21 12 is: 50: 12,798 - the INFO -> Line 57 is worker_consumer AT: 
 . 5 Task Status Finished IS Success 
2019-12-21 12 is: 50: 12,798 - the INFO -> AT worker_consumer line 58: 
 king we had finished five quail eggs, some more of it 
2019-12-21 12: 50: 12,799 - INFO -> producer at line 70: 
 children are all, plus seven quail eggs may notice nor eat 
2019-12 -21 12: 50: 12,799 - INFO -> producer at line 70:
 Children are all, plus eight quail eggs notification can eat them 
2019-12-21 12: 50: 12,799 - INFO -> producer at line 70: 
 children are all, plus a notification under 9 quail eggs can eat them 
2019-12- 21 12: 50: 12,799 - INFO -> producer at line 70: 
 children are, the notification may be added nor eat quail eggs 10 
2019-12-21 12 is: 50: 12,801 - the INFO -> do_work aT Line 22 is: 
 do . 7 something, Start Time Sat On Dec 21 is 12:50:12 2019 
2019-12-21 12 is: 50: 14,802 - the INFO -> Line 57 is worker_consumer AT: 
 . 7 Task Status Finished IS Success 
2019-12-21 12 is: 50: 14,802 - INFO -> worker_consumer at line 58 : 
 king we finished 7 quail eggs, some more of it 
2019-12-21 12: 50: 14,804 - INFO -> do_work AT Line 22: 
 do something 8, Start Time Sat Dec 21 2019 12:50:14 
2019-12-21 12: 50: 17,805 - INFO -> worker_consumer AT Line 57:
 Finished IS Success Task Status 8 
2019-12-21 12: 50: 17,805 - INFO -> worker_consumer AT Line 58: 
 King we finished 8 quail eggs, some more of it 
2019-12-21 12: 50: 17,806 - INFO - -> do_work AT Line 22 is: 
 do something. 9, Start Time Sat On Dec 21 is 12:50:17 2019 
2019-12-21 12 is: 50: 20,808 - the INFO -> Line 57 is worker_consumer AT: 
 . 9 Task Status Finished IS failed 
2019- 12-21 12: 50: 20,808 - INFO -> worker_consumer at line 58: 
 king we had finished the 9 quail eggs, some more of it 
2019-12-21 12: 50: 20,810 - INFO -> do_work AT Line 22: 
 do 10 something, Start Time Sat On Dec 21 is 12:50:20 2019 
2019-12-21 12 is: 50: 23,810 - the INFO -> Line 57 is worker_consumer AT: 
 10 IS failed Task Status Finished
2019-12-21 12: 50: 23,810 - INFO -> worker_consumer at line 58: 
 King we had finished the 10 quail eggs, some more of it

  

Guess you like

Origin www.cnblogs.com/SunshineKimi/p/12076618.html