Fallback blocking event loop for transports that doesn't support AIO.
(obj, connection, consumer, blueprint, hub, qos,
heartbeat, clock, hbrate=2.0, **kwargs)
| 159 | |
| 160 | |
| 161 | def synloop(obj, connection, consumer, blueprint, hub, qos, |
| 162 | heartbeat, clock, hbrate=2.0, **kwargs): |
| 163 | """Fallback blocking event loop for transports that doesn't support AIO.""" |
| 164 | RUN = bootsteps.RUN |
| 165 | on_task_received = obj.create_task_handler() |
| 166 | perform_pending_operations = obj.perform_pending_operations |
| 167 | heartbeat_error = [None] |
| 168 | dispatcher_heartbeat_error = [None] |
| 169 | is_green = getattr(obj.pool, 'is_green', False) |
| 170 | if is_green: |
| 171 | heartbeat_error = _enable_amqheartbeats(obj.timer, connection, rate=hbrate) |
| 172 | if obj.event_dispatcher: |
| 173 | dispatcher_heartbeat_error = _enable_amqheartbeats( |
| 174 | obj.timer, obj.event_dispatcher.connection, rate=hbrate) |
| 175 | consumer.on_message = on_task_received |
| 176 | consumer.consume() |
| 177 | |
| 178 | obj.on_ready() |
| 179 | |
| 180 | def _loop_cycle(): |
| 181 | """ |
| 182 | Perform one iteration of the blocking event loop. |
| 183 | """ |
| 184 | if heartbeat_error[0] is not None: |
| 185 | raise heartbeat_error[0] |
| 186 | if dispatcher_heartbeat_error[0] is not None: |
| 187 | raise dispatcher_heartbeat_error[0] |
| 188 | if qos.prev != qos.value: |
| 189 | qos.update() |
| 190 | try: |
| 191 | perform_pending_operations() |
| 192 | connection.drain_events(timeout=2.0) |
| 193 | except socket.timeout: |
| 194 | pass |
| 195 | except OSError: |
| 196 | if blueprint.state == RUN: |
| 197 | raise |
| 198 | # drain this connection so broker heartbeats are consumed. |
| 199 | if is_green and obj.amqheartbeat and obj.event_dispatcher: |
| 200 | connection_ = obj.event_dispatcher.connection |
| 201 | if connection_ and connection_.supports_heartbeats: |
| 202 | try: |
| 203 | _quick_drain(obj.event_dispatcher.connection) |
| 204 | except OSError: |
| 205 | if blueprint.state == RUN: |
| 206 | raise |
| 207 | |
| 208 | try: |
| 209 | while blueprint.state == RUN and obj.connection: |
| 210 | try: |
| 211 | state.maybe_shutdown() |
| 212 | finally: |
| 213 | _loop_cycle() |
| 214 | except Exception: |
| 215 | # Reset the hub on error (e.g. connection loss) to clean up |
| 216 | # stale state from the old connection, matching the cleanup |
| 217 | # already done in asynloop. Without this, the synloop |
| 218 | # (used by gevent/eventlet pools) could leave stale callbacks |