<s> import threading <EOL> import collections <EOL> import requests <EOL> class FlushThread ( threading . Thread ) : <EOL> def __init__ ( self , client ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . client = client <EOL> def run ( self ) : <EOL> self . client . sync_flush ( ) <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , num_threads = <NUM_LIT:3> ) : <EOL> self . queue = collections . deque ( ) <EOL> self . flush_lock = threading . Lock ( ) <EOL> self . num_threads = num_threads <EOL> self . flush_threads = [ FlushThread ( self ) for _ in range ( self . num_threads ) ] <EOL> self . total_sent = <NUM_LIT:0> <EOL> def enqueue ( self , method , * args , ** kwargs ) : <EOL> self . queue . append ( ( method , args , kwargs ) ) <EOL> self . refresh_threads ( ) <EOL> def get ( self , * args , ** kwargs ) : <EOL> self . enqueue ( '<STR_LIT>' , * args , ** kwargs ) <EOL> def post ( self , * args , ** kwargs ) : <EOL> self . enqueue ( '<STR_LIT>' , * args , ** kwargs ) <EOL> def put ( self , * args , ** kwargs ) : <EOL> self . enqueue ( '<STR_LIT>' , * args , ** kwargs ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> self . enqueue ( '<STR_LIT>' , * args , ** kwargs ) <EOL> def refresh_threads ( self ) : <EOL> with self . flush_lock : <EOL> if len ( self . queue ) > <NUM_LIT:0> : <EOL> to_refresh = [ index for index , thread in enumerate ( self . flush_threads ) if not thread . is_alive ( ) ] <EOL> for index in to_refresh : <EOL> self . flush_threads [ index ] = FlushThread ( self ) <EOL> self . flush_threads [ index ] . start ( ) <EOL> def sync_flush ( self ) : <EOL> while len ( self . queue ) > <NUM_LIT:0> : <EOL> method , args , kwargs = self . queue . pop ( ) <EOL> getattr ( requests , method ) ( * args , ** kwargs ) <EOL> self . total_sent += <NUM_LIT:1> </s>
