def test_channel(): channel = Channel(uri=URI, exchange=EXCHANGE) subscription = Subscription(channel) subscription.subscribe("MyTopic.Sub.Sub") struct = Struct() struct.fields["value"].number_value = 666.0 sent = Message(struct) sent.reply_to = subscription sent.created_at = int(1000 * now()) / 1000.0 sent.timeout = 1.0 sent.topic = "MyTopic.Sub.Sub" channel.publish(message=sent) received = channel.consume(timeout=1.0) assert sent.reply_to == received.reply_to assert sent.subscription_id == received.subscription_id assert sent.content_type == received.content_type assert sent.body == received.body assert sent.status == received.status assert sent.topic == received.topic assert sent.correlation_id == received.correlation_id assert sent.timeout == received.timeout assert sent.metadata == received.metadata assert sent.created_at == received.created_at assert str(sent) == str(received) struct2 = received.unpack(Struct) assert str(struct) == str(struct2) assert struct == struct2 channel.close()
def request(self, content, topic, timeout_ms, metadata=None): if not self.can_request(): raise Exception( "Can't request more than {}. Use 'RequestManager.can_request' " "method to check if you can do requests.") tracer = Tracer( exporter=self._zipkin_exporter) if self._do_tracing else None span = tracer.start_span(name='request') if self._do_tracing else None msg = Message(content=content) msg.topic = topic msg.reply_to = self._subscription msg.timeout = timeout_ms / 1000.0 self._log.debug("[Sending] metadata={}, cid={}", metadata, msg.correlation_id) if self._do_tracing: for key, value in (metadata or {}).items(): span.add_attribute(key, value) tracer.end_span() msg.inject_tracing(span) self._publish(msg, metadata) if len(self._requests) >= self._max_requests: self._can_request = False
def consume_ready(self, timeout=1.0): received_msgs = [] # wait for new message try: stated_at = now() while True: _timeout = max(0.0, stated_at + timeout - now()) msg = self._channel.consume(timeout=_timeout) if msg.status.ok() and msg.has_correlation_id(): cid = msg.correlation_id if cid in self._requests: received_msgs.append( (msg, self._requests[cid]["metadata"])) del self._requests[cid] except socket.timeout: pass # check for timeouted requests for cid in self._requests.keys(): timeouted_msg = self._requests[cid]["msg"] if timeouted_msg.deadline_exceeded(): msg = Message() msg.body = timeouted_msg.body msg.topic = timeouted_msg.topic msg.reply_to = self._subscription msg.timeout = timeouted_msg.timeout metadata = self._requests[cid]["metadata"] del self._requests[cid] self._log.debug("[Retring] metadata={}, cid={}", metadata, msg.correlation_id) self._publish(msg, metadata) if not self._can_request and len(self._requests) <= self._min_requests: self._can_request = True return received_msgs
frame_fetcher = FrameVideoFetcher(video_files=pending_videos, base_folder=options.folder) while True: if state == State.MAKE_REQUESTS: state = State.RECV_REPLIES if len(requests) < MIN_REQUESTS: while len(requests) <= MAX_REQUESTS: base_name, frame_id, frame = frame_fetcher.next() if frame is None: if len(requests) == 0: state = State.EXIT break pb_image = make_pb_image(frame) msg = Message(content=pb_image, reply_to=subscription) msg.timeout = DEADLINE_SEC channel.publish(msg, topic='SkeletonsDetector.Detect') requests[msg.correlation_id] = { 'content': pb_image, 'base_name': base_name, 'frame_id': frame_id, 'requested_at': time.time() } continue elif state == State.RECV_REPLIES: try: msg = channel.consume(timeout=1.0) if msg.status.ok(): annotations = msg.unpack(ObjectAnnotations)