#NEWLINE# def timeout_thread( queue ) : #NEWLINE# #INDENT# 'Terminate threads by timeout.\n\n    Function need to be run in separate thread. Its designed to terminate\n    threads which are running longer then timeout.\n\n    Parent thread will put tuples (thread_ident, deadline) in the queue,\n    where `thread_ident` is Thread.ident value of thread to watch, and\n    `deadline` is timestamp when thread should be terminated. Also tuple\n    (None, None) should be put when all threads are exited and no more\n    threads to watch.\n\n    :param queue: Queue object to communicate with parent thread.\n    ' #NEWLINE# all_threads = collections . deque ( ) #NEWLINE# while True : #NEWLINE# #INDENT# if ( not all_threads ) : #NEWLINE# #INDENT# timeout = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( thread_ident , deadline ) = all_threads [ 0 ] #NEWLINE# timeout = ( deadline - time . time ( ) ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# next_thread = queue . get ( timeout = timeout ) #NEWLINE# all_threads . append ( next_thread ) #NEWLINE# #UNINDENT# except ( moves . queue . Empty , ValueError ) : #NEWLINE# #INDENT# LOG . info ( ( 'Thread %s is timed out. Terminating.' % thread_ident ) ) #NEWLINE# terminate_thread ( thread_ident ) #NEWLINE# all_threads . popleft ( ) #NEWLINE# #UNINDENT# if ( next_thread == ( None , None ) ) : #NEWLINE# #INDENT# return