#NEWLINE# def run_sync( self , func , timeout = None ) : #NEWLINE# #INDENT# "Starts the `IOLoop`, runs the given function, and stops the loop.\n\n        If the function returns a `.Future`, the `IOLoop` will run\n        until the future is resolved.  If it raises an exception, the\n        `IOLoop` will stop and the exception will be re-raised to the\n        caller.\n\n        The keyword-only argument ``timeout`` may be used to set\n        a maximum duration for the function.  If the timeout expires,\n        a `TimeoutError` is raised.\n\n        This method is useful in conjunction with `tornado.gen.coroutine`\n        to allow asynchronous calls in a ``main()`` function::\n\n            @gen.coroutine\n            def main():\n                # do stuff...\n\n            if __name__ == '__main__':\n                IOLoop.instance().run_sync(main)\n        " #NEWLINE# future_cell = [ None ] #NEWLINE# def run( ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# result = func ( ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# future_cell [ 0 ] = TracebackFuture ( ) #NEWLINE# future_cell [ 0 ] . set_exc_info ( sys . exc_info ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if is_future ( result ) : #NEWLINE# #INDENT# future_cell [ 0 ] = result #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# future_cell [ 0 ] = TracebackFuture ( ) #NEWLINE# future_cell [ 0 ] . set_result ( result ) #NEWLINE# #UNINDENT# #UNINDENT# self . add_future ( future_cell [ 0 ] , ( lambda future : self . stop ( ) ) ) #NEWLINE# #UNINDENT# self . add_callback ( run ) #NEWLINE# if ( timeout is not None ) : #NEWLINE# #INDENT# timeout_handle = self . add_timeout ( ( self . time ( ) + timeout ) , self . stop ) #NEWLINE# #UNINDENT# self . start ( ) #NEWLINE# if ( timeout is not None ) : #NEWLINE# #INDENT# self . remove_timeout ( timeout_handle ) #NEWLINE# #UNINDENT# if ( not future_cell [ 0 ] . done ( ) ) : #NEWLINE# #INDENT# raise TimeoutError ( ( 'Operation timed out after %s seconds' % timeout ) ) #NEWLINE# #UNINDENT# return future_cell [ 0 ] . result ( )