<s> from threading import Thread <EOL> import sys <EOL> import os <EOL> import Queue <EOL> from multiprocess import pool , Process <EOL> import multiprocessing <EOL> from binder . app import App <EOL> from binder . utils import make_dir <EOL> NUM_WORKERS = <NUM_LIT:5> <EOL> GET_TIMEOUT = <NUM_LIT:2> <EOL> LOG_DIR = "<STR_LIT>" <EOL> class NoDaemonProcess ( Process ) : <EOL> def _get_daemon ( self ) : <EOL> return False <EOL> def _set_daemon ( self , value ) : <EOL> pass <EOL> daemon = property ( _get_daemon , _set_daemon ) <EOL> class NoDaemonPool ( pool . Pool ) : <EOL> Process = NoDaemonProcess <EOL> def build_app ( spec , log_dir , preload = False ) : <EOL> name = spec [ "<STR_LIT:name>" ] <EOL> app = App . get_app ( name ) <EOL> if app and app . build_state == App . BuildState . BUILDING : <EOL> print ( "<STR_LIT>" . format ( name ) ) <EOL> return <EOL> new_app = App . create ( spec ) <EOL> try : <EOL> new_app . build ( preload = preload ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" . format ( new_app . name , e ) ) <EOL> App . index . update_build_state ( App . BuildState . FAILED ) <EOL> class Builder ( Thread ) : <EOL> def __init__ ( self , queue , preload ) : <EOL> super ( Builder , self ) . __init__ ( ) <EOL> self . _build_queue = queue <EOL> self . _pool = NoDaemonPool ( processes = NUM_WORKERS ) <EOL> multiprocessing . freeze_support ( ) <EOL> self . _stopped = False <EOL> self . _preload = preload <EOL> make_dir ( LOG_DIR ) <EOL> def stop ( self ) : <EOL> self . _stopped = True <EOL> self . _pool . terminate ( ) <EOL> def _build ( self , spec ) : <EOL> try : <EOL> pool = self . _pool <EOL> pool . apply_async ( build_app , ( spec , LOG_DIR , self . _preload ) ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" . format ( e ) ) <EOL> def run ( self ) : <EOL> while not self . _stopped : <EOL> try : <EOL> next_job = self . _build_queue . get ( timeout = GET_TIMEOUT ) <EOL> print ( "<STR_LIT>" . format ( next_job ) ) <EOL> self . _build ( next_job ) <EOL> except Queue . Empty : <EOL> pass </s>
