MCPcopy
hub / github.com/alievk/avatarify-python / _send_recv_async

Method _send_recv_async

afy/predictor_remote.py:70–125  ·  view source on GitHub ↗
(self, method, args, critical)

Source from the content-addressed store, hash-verified

68 return lambda *args, **kwargs: self._send_recv_async(attr, (args, kwargs), critical=is_critical)
69
70 def _send_recv_async(self, method, args, critical):
71 self._i_msg += 1
72
73 args, kwargs = args
74
75 tt = TicToc()
76 tt.tic()
77 if method == 'predict':
78 image = args[0]
79 assert isinstance(image, np.ndarray), 'Expected image'
80 ret_code, data = cv2.imencode(".jpg", image, [int(cv2.IMWRITE_JPEG_QUALITY), opt.jpg_quality])
81 else:
82 data = msgpack.packb((args, kwargs))
83 self.timing.add('PACK', tt.toc())
84
85 meta = {
86 'name': method,
87 'critical': critical,
88 'id': self._i_msg
89 }
90
91 self.log("send", meta)
92
93 if critical:
94 self.send_queue.put((meta, data))
95
96 while True:
97 meta_recv, data_recv = self.recv_queue.get()
98 if meta_recv == meta:
99 break
100 else:
101 try:
102 # TODO: find good timeout
103 self.send_queue.put((meta, data), timeout=PUT_TIMEOUT)
104 except queue.Full:
105 self.log('send_queue is full')
106
107 try:
108 meta_recv, data_recv = self.recv_queue.get(timeout=GET_TIMEOUT)
109 except queue.Empty:
110 self.log('recv_queue is empty')
111 return None
112
113 self.log("recv", meta_recv)
114
115 tt.tic()
116 if meta_recv['name'] == 'predict':
117 result = cv2.imdecode(np.frombuffer(data_recv, dtype='uint8'), -1)
118 else:
119 result = msgpack.unpackb(data_recv)
120 self.timing.add('UNPACK', tt.toc())
121
122 if opt.verbose:
123 Once(self.timing, per=1)
124
125 return result
126
127 @staticmethod

Callers 2

init_remote_workerMethod · 0.95
__getattr__Method · 0.95

Calls 5

ticMethod · 0.95
tocMethod · 0.95
TicTocClass · 0.90
OnceClass · 0.90
addMethod · 0.80

Tested by

no test coverage detected