<s> from __future__ import print_function <EOL> import os <EOL> import signal <EOL> import time <EOL> from alligator . constants import ALL <EOL> class Worker ( object ) : <EOL> def __init__ ( self , gator , max_tasks = <NUM_LIT:0> , to_consume = ALL , nap_time = <NUM_LIT:0.1> ) : <EOL> """<STR_LIT>""" <EOL> self . gator = gator <EOL> self . max_tasks = int ( max_tasks ) <EOL> self . to_consume = to_consume <EOL> self . nap_time = nap_time <EOL> self . tasks_complete = <NUM_LIT:0> <EOL> def ident ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( os . getpid ( ) ) <EOL> def starting ( self ) : <EOL> """<STR_LIT>""" <EOL> ident = self . ident ( ) <EOL> print ( '<STR_LIT>' . format ( ident , self . to_consume ) ) <EOL> if self . max_tasks : <EOL> print ( '<STR_LIT>' . format ( ident , self . max_tasks ) ) <EOL> else : <EOL> print ( '<STR_LIT>' . format ( ident ) ) <EOL> def interrupt ( self ) : <EOL> """<STR_LIT>""" <EOL> ident = self . ident ( ) <EOL> print ( '<STR_LIT>' . format ( <EOL> ident , <EOL> self . to_consume <EOL> ) ) <EOL> def stopping ( self ) : <EOL> """<STR_LIT>""" <EOL> ident = self . ident ( ) <EOL> print ( '<STR_LIT>' . format ( <EOL> ident , <EOL> self . to_consume , <EOL> self . tasks_complete <EOL> ) ) <EOL> def result ( self , result ) : <EOL> """<STR_LIT>""" <EOL> print ( result ) <EOL> def run_forever ( self ) : <EOL> """<STR_LIT>""" <EOL> self . starting ( ) <EOL> self . keep_running = True <EOL> def handle ( signum , frame ) : <EOL> self . interrupt ( ) <EOL> self . keep_running = False <EOL> signal . signal ( signal . SIGINT , handle ) <EOL> while self . keep_running : <EOL> if self . max_tasks and self . tasks_complete >= self . max_tasks : <EOL> self . stopping ( ) <EOL> if self . gator . len ( ) : <EOL> result = self . gator . pop ( ) <EOL> self . tasks_complete += <NUM_LIT:1> <EOL> self . result ( result ) <EOL> if self . nap_time >= <NUM_LIT:0> : <EOL> time . sleep ( self . nap_time ) <EOL> return <NUM_LIT:0> </s>
