def run_callback_threadsafe(loop, callback, *args):
 ident = loop.__dict__.get('_thread_ident')
 if ((ident is not None) and (ident == threading.get_ident())):
  raise RuntimeError('Cannot be called from within the event loop')
 future = concurrent.futures.Future()
 def run_callback():
  'Run callback and store result.'
  try:
   future.set_result(callback(*args))
  except Exception as exc:
   if future.set_running_or_notify_cancel():
    future.set_exception(exc)
   else:
    _LOGGER.warning('Exception on lost future: ', exc_info=True)
 loop.call_soon_threadsafe(run_callback)
 return future

def run_callback_threadsafe ( loop , callback , * args ) : ident = loop . __dict__ . get ( '_thread_ident' ) if ( ( ident is not None ) and ( ident == threading . get_ident ( ) ) ) : raise RuntimeError ( 'Cannot be called from within the event loop' ) future = concurrent . futures . Future ( ) def run_callback ( ) : try : future . set_result ( callback ( * args ) ) except Exception as exc : if future . set_running_or_notify_cancel ( ) : future . set_exception ( exc ) else : _LOGGER . warning ( 'Exception on lost future: ' , exc_info = True ) loop . call_soon_threadsafe ( run_callback ) return future
submit a callback object to a given event loop .