nodes: 0, module; 1, function_definition; 2, function_name:poll_events; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, while_statement; 8, comment:"""Continuously polls all types of events from sl4a.
  
          Events are sorted by name and store in separate queues.
          If there are registered handlers, the handlers will be called with
          corresponding event immediately upon event discovery, and the event
          won't be stored. If exceptions occur, stop the dispatcher and return
          """; 9, attribute; 10, block; 11, identifier:self; 12, identifier:started; 13, expression_statement; 14, expression_statement; 15, try_statement; 16, if_statement; 17, comment:# if handler registered, process event; 18, if_statement; 19, if_statement; 20, assignment; 21, assignment; 22, block; 23, except_clause; 24, not_operator; 25, block; 26, elif_clause; 27, else_clause; 28, comparison_operator:event_name in self.handlers; 29, block; 30, comparison_operator:event_name == "EventDispatcherShutdown"; 31, block; 32, else_clause; 33, identifier:event_obj; 34, None; 35, identifier:event_name; 36, None; 37, expression_statement; 38, block; 39, identifier:event_obj; 40, continue_statement; 41, comparison_operator:'name' not in event_obj; 42, block; 43, block; 44, identifier:event_name; 45, attribute; 46, expression_statement; 47, identifier:event_name; 48, string:"EventDispatcherShutdown"; 49, expression_statement; 50, break_statement; 51, block; 52, assignment; 53, if_statement; 54, string; 55, identifier:event_obj; 56, expression_statement; 57, continue_statement; 58, expression_statement; 59, identifier:self; 60, identifier:handlers; 61, call; 62, call; 63, expression_statement; 64, if_statement; 65, expression_statement; 66, identifier:event_obj; 67, call; 68, attribute; 69, block; 70, string_content:name; 71, call; 72, assignment; 73, attribute; 74, argument_list; 75, attribute; 76, argument_list; 77, call; 78, comparison_operator:event_name in self.event_dict; 79, comment:# otherwise, cache event; 80, block; 81, else_clause; 82, call; 83, attribute; 84, argument_list; 85, identifier:self; 86, identifier:started; 87, expression_statement; 88, expression_statement; 89, raise_statement; 90, identifier:print; 91, argument_list; 92, identifier:event_name; 93, subscript; 94, identifier:self; 95, identifier:handle_subscribed_event; 96, identifier:event_obj; 97, identifier:event_name; 98, attribute; 99, identifier:closeSl4aSession; 100, attribute; 101, argument_list; 102, identifier:event_name; 103, attribute; 104, expression_statement; 105, block; 106, attribute; 107, argument_list; 108, attribute; 109, identifier:eventWait; 110, integer:50000; 111, call; 112, call; 113, call; 114, identifier:event_obj; 115, string; 116, identifier:self; 117, identifier:_sl4a; 118, attribute; 119, identifier:acquire; 120, identifier:self; 121, identifier:event_dict; 122, call; 123, expression_statement; 124, expression_statement; 125, expression_statement; 126, attribute; 127, identifier:release; 128, identifier:self; 129, identifier:_sl4a; 130, identifier:print; 131, argument_list; 132, identifier:print; 133, argument_list; 134, attribute; 135, argument_list; 136, string_content:name; 137, identifier:self; 138, identifier:lock; 139, attribute; 140, argument_list; 141, assignment; 142, call; 143, assignment; 144, identifier:self; 145, identifier:lock; 146, string:"Exception happened during polling."; 147, call; 148, string:"Received Malformed event {}"; 149, identifier:format; 150, identifier:event_obj; 151, subscript; 152, identifier:put; 153, identifier:event_obj; 154, identifier:q; 155, call; 156, attribute; 157, argument_list; 158, subscript; 159, identifier:q; 160, attribute; 161, argument_list; 162, attribute; 163, identifier:event_name; 164, attribute; 165, argument_list; 166, identifier:q; 167, identifier:put; 168, identifier:event_obj; 169, attribute; 170, identifier:event_name; 171, identifier:traceback; 172, identifier:format_exc; 173, identifier:self; 174, identifier:event_dict; 175, identifier:queue; 176, identifier:Queue; 177, identifier:self; 178, identifier:event_dict
code: def poll_events(self):
          """Continuously polls all types of events from sl4a.
  
          Events are sorted by name and store in separate queues.
          If there are registered handlers, the handlers will be called with
          corresponding event immediately upon event discovery, and the event
          won't be stored. If exceptions occur, stop the dispatcher and return
          """
          while self.started:
              event_obj = None
              event_name = None
              try:
                  event_obj = self._sl4a.eventWait(50000)
              except:
                  if self.started:
                      print("Exception happened during polling.")
                      print(traceback.format_exc())
                      raise
              if not event_obj:
                  continue
              elif 'name' not in event_obj:
                  print("Received Malformed event {}".format(event_obj))
                  continue
              else:
                  event_name = event_obj['name']
              # if handler registered, process event
              if event_name in self.handlers:
                  self.handle_subscribed_event(event_obj, event_name)
              if event_name == "EventDispatcherShutdown":
                  self._sl4a.closeSl4aSession()
                  break
              else:
                  self.lock.acquire()
                  if event_name in self.event_dict:  # otherwise, cache event
                      self.event_dict[event_name].put(event_obj)
                  else:
                      q = queue.Queue()
                      q.put(event_obj)
                      self.event_dict[event_name] = q
                  self.lock.release()