Python: producer and consumer model, python producer model

Source: Internet
Author: User

Python: producer and consumer model, python producer model

1. the contradiction between producer and consumer models lies in the imbalance of data supply and demand.

Import timeimport randomfrom multiprocessing import Queuefrom multiprocessing import Processdef producer (q, food): for I in range (5): q. put ('% s-% s' % (food, I) print (' produced % s' % food) time. sleep (random. random () q. put (None) q. put (None) q. put (None) # Three consumers need three signals def consumer (q, name): while True: food = q. get () if food = None: break print ('% s has eaten % s' % (name, food )) if _ name _ = '_ main _': q = Queue () p1 = Process (target = producer, args = (q, 'jackfruit dry ')) p1.start () p2 = Process (target = producer, args = (q, 'yogurt ') p2.start () c1 = Process (target = consumer, args = (q, 'rabbit ') c1.start () c2 = Process (target = consumer, args = (q, 'orangecat') c2.start () c3 = Process (target = consumer, args = (q, 'cuihua') c3.start ()
Producer and consumer model
1. The amount of data to be processed by the consumer is uncertain.
2. Therefore, only the while loop can be used to process data, but the while LOOP cannot end.
3. The producer needs to send signals.
4. How many consumers need to send signals
5. However, the number of signals sent must be calculated based on the number of producers and consumers, which is inconvenient.
2, JoinableQueue
Import timeimport randomfrom multiprocessing import Processfrom multiprocessing import JoinableQueuedef producer (q, food): for I in range (5): q. put ('% s-% s' % (food, I) print (' produced % s' % food) time. sleep (random. random () q. join () # Wait for the consumer to finish processing all the data def consumer (q, name): while True: food = q. get () # producer does not produce or produce slow print ('% s eat % s' % (name, food) q. task_done () # JoinableQueue provides the internal counting function. Each execution of task_done reduces the count by one.
If _ name _ = '_ main __':
Q = JoinableQueue ()
P1 = Process (target = producer, args = (q, 'fried river powder '))
P1.start ()
P2 = Process (target = producer, args = (q, 'strawberry '))
P2.start ()
C1 = Process (target = consumer, args = (q, 'rabbit '))
C1.daemon = True
C1.start ()
C2 = Process (target = consumer, args = (q, 'Orange _ cat '))
C2.daemon = True
C2.start ()
C3 = Process (target = consumer, args = (q, 'Teddy '))
C3.daemon = True
C3.start ()

P1.join () # Wait for p1 to complete execution
P2.join () # Wait For p2 to finish executing


All data produced by the producer is consumed-the producer process ends-The Master Process Code Execution ends-the consumer daemon process ends

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.