Non-blocking event loop.
(obj, connection, consumer, blueprint, hub, qos,
heartbeat, clock, hbrate=2.0)
| 63 | |
| 64 | |
| 65 | def asynloop(obj, connection, consumer, blueprint, hub, qos, |
| 66 | heartbeat, clock, hbrate=2.0): |
| 67 | """Non-blocking event loop.""" |
| 68 | RUN = bootsteps.RUN |
| 69 | update_qos = qos.update |
| 70 | errors = connection.connection_errors |
| 71 | |
| 72 | on_task_received = obj.create_task_handler() |
| 73 | |
| 74 | # The consumer records the timer entries that existed before it started |
| 75 | # connecting, so that the entries registered by the transport when |
| 76 | # connecting are cancelled along with the ones registered below. |
| 77 | timer_entries_before = obj.timer_entries_before_start |
| 78 | if timer_entries_before is None: |
| 79 | timer_entries_before = _timer_entries(hub.timer) |
| 80 | |
| 81 | heartbeat_error = _enable_amqheartbeats(hub.timer, connection, rate=hbrate) |
| 82 | dispatcher_heartbeat_error = [None] |
| 83 | if obj.event_dispatcher: |
| 84 | dispatcher_heartbeat_error = _enable_amqheartbeats( |
| 85 | hub.timer, obj.event_dispatcher.connection, rate=hbrate) |
| 86 | |
| 87 | consumer.on_message = on_task_received |
| 88 | obj.controller.register_with_event_loop(hub) |
| 89 | obj.register_with_event_loop(hub) |
| 90 | registered_timer_entries = ( |
| 91 | _timer_entries(hub.timer) - timer_entries_before) |
| 92 | # The consumer cancels these if the connection fails before the loop |
| 93 | # below is running (see :meth:`Consumer.start`). |
| 94 | obj.timer_entries_registered = registered_timer_entries |
| 95 | consumer.consume() |
| 96 | obj.on_ready() |
| 97 | |
| 98 | # did_start_ok will verify that pool processes were able to start, |
| 99 | # but this will only work the first time we start, as |
| 100 | # maxtasksperchild will mess up metrics. |
| 101 | if not obj.restart_count and not obj.pool.did_start_ok(): |
| 102 | raise WorkerLostError('Could not start worker processes') |
| 103 | |
| 104 | # consumer.consume() may have prefetched up to our |
| 105 | # limit - drain an event so we're in a clean state |
| 106 | # prior to starting our event loop. |
| 107 | if connection.transport.driver_type == 'amqp': |
| 108 | hub.call_soon(_quick_drain, connection) |
| 109 | |
| 110 | # FIXME: Use loop.run_forever |
| 111 | # Tried and works, but no time to test properly before release. |
| 112 | hub.propagate_errors = errors |
| 113 | loop = hub.create_loop() |
| 114 | |
| 115 | try: |
| 116 | while blueprint.state == RUN and obj.connection: |
| 117 | state.maybe_shutdown() |
| 118 | if heartbeat_error[0] is not None: |
| 119 | raise heartbeat_error[0] |
| 120 | if dispatcher_heartbeat_error[0] is not None: |
| 121 | raise dispatcher_heartbeat_error[0] |
| 122 |