<s> __author__ = """<STR_LIT>""" <EOL> from Queue import Queue <EOL> import threading <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from os import getcwd <EOL> from os import sys <EOL> sys . path . append ( getcwd ( ) ) <EOL> from MOAL . helpers . text import gibberish <EOL> from MOAL . helpers . display import Section <EOL> COUNT_LOCK = threading . Lock ( ) <EOL> DEBUG = True if __name__ == '<STR_LIT:__main__>' else False <EOL> total = <NUM_LIT:1> <EOL> class CountThread ( threading . Thread ) : <EOL> def start ( self , name ) : <EOL> self . name = name <EOL> super ( CountThread , self ) . start ( ) <EOL> def run ( self ) : <EOL> global total <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> COUNT_LOCK . acquire ( ) <EOL> total += <NUM_LIT:1> <EOL> COUNT_LOCK . release ( ) <EOL> if DEBUG : <EOL> print ( '<STR_LIT>' . format ( self . name , total ) ) <EOL> class Worker : <EOL> def __init__ ( self , queue , num_threads = <NUM_LIT:2> ) : <EOL> self . NUM_THREADS = num_threads <EOL> self . queue = queue <EOL> def do_work ( self , item ) : <EOL> if DEBUG : <EOL> print ( '<STR_LIT>' . format ( item ) ) <EOL> def worker ( self ) : <EOL> while True : <EOL> item = self . queue . get ( ) <EOL> self . do_work ( item ) <EOL> self . queue . task_done ( ) <EOL> def process_all ( self ) : <EOL> if DEBUG : <EOL> print ( '<STR_LIT>' ) <EOL> for _ in range ( self . NUM_THREADS ) : <EOL> t = threading . Thread ( target = self . worker ) <EOL> t . daemon = True <EOL> t . start ( ) <EOL> class Producer : <EOL> def __init__ ( self ) : <EOL> self . items = [ ] <EOL> def __iter__ ( self ) : <EOL> for record in self . items : <EOL> yield record <EOL> raise StopIteration <EOL> def add ( self ) : <EOL> self . items . append ( self . make ( ) ) <EOL> def make ( self ) : <EOL> return gibberish ( ) <EOL> if DEBUG : <EOL> with Section ( '<STR_LIT>' ) : <EOL> counter_one = CountThread ( ) <EOL> counter_two = CountThread ( ) <EOL> counter_three = CountThread ( ) <EOL> counter_one . start ( '<STR_LIT>' ) <EOL> counter_two . start ( '<STR_LIT>' ) <EOL> counter_three . start ( '<STR_LIT>' ) <EOL> with Section ( '<STR_LIT>' ) : <EOL> work_queue = Queue ( ) <EOL> bot = Worker ( work_queue ) <EOL> producer = Producer ( ) <EOL> for _ in range ( <NUM_LIT:10> ) : <EOL> producer . add ( ) <EOL> for record in producer : <EOL> print ( '<STR_LIT>' . format ( record ) ) <EOL> work_queue . put ( record ) <EOL> bot . process_all ( ) <EOL> work_queue . join ( ) </s>
