#NEWLINE# def answer( outerself , inline_query , compute_fn , * compute_args , **compute_kwargs ) : #NEWLINE# #INDENT# from_id = inline_query [ 'from' ] [ 'id' ] #NEWLINE# class Worker ( threading . Thread ) : #NEWLINE# #INDENT# def __init__( innerself ) : #NEWLINE# #INDENT# super ( Worker , innerself ) . __init__ ( ) #NEWLINE# innerself . _cancelled = False #NEWLINE# #UNINDENT# def cancel( innerself ) : #NEWLINE# #INDENT# innerself . _cancelled = True #NEWLINE# #UNINDENT# def run( innerself ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# query_id = inline_query [ 'id' ] #NEWLINE# if innerself . _cancelled : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# ans = compute_fn ( * compute_args , ** compute_kwargs ) #NEWLINE# if innerself . _cancelled : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# if isinstance ( ans , list ) : #NEWLINE# #INDENT# outerself . _bot . answerInlineQuery ( query_id , ans ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( ans , tuple ) : #NEWLINE# #INDENT# outerself . _bot . answerInlineQuery ( query_id , * ans ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( ans , dict ) : #NEWLINE# #INDENT# outerself . _bot . answerInlineQuery ( query_id , ** ans ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( 'Invalid answer format' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# with outerself . _lock : #NEWLINE# #INDENT# if ( not innerself . _cancelled ) : #NEWLINE# #INDENT# del outerself . _workers [ from_id ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# with outerself . _lock : #NEWLINE# #INDENT# if ( from_id in outerself . _workers ) : #NEWLINE# #INDENT# outerself . _workers [ from_id ] . cancel ( ) #NEWLINE# #UNINDENT# outerself . _workers [ compute_fn ] = Worker ( ) #NEWLINE# outerself . _workers [ from_id ] . start ( )