Jackiexiao
Jackiexiao

Reputation: 888

how to send msg from generator in pyzmq?

from : https://github.com/zeromq/pyzmq/issues/1290

def encap_msg(msg):
    yield b'Procotol v1`
    yield uuid.uuid4().bytes
    yield from msg

socket.send_multipart(encap_msg(msg))

When I ran this snippet part of code, it raised an error.

File "xxxxxx/zmq/sugar/socket.py", line 439, in send_multipart
    for msg in msg_parts[:-1]:
'generator' object is not subscriptable

According to the document, msg_parts is iterable, and generators are iterable.

for example, in TTS, request is text, response is stream audio, how could I send audio frame one by one from generator using pyzmq/

Upvotes: 0

Views: 79

Answers (1)

user3666197
user3666197

Reputation: 1

Q : how to send msg from generator in pyzmq?

A :
until the pyzmq-language wrapper starts to provide a working code for this kind of syntax-sugaring, which it does not seem to do so far, as seen in 2022-Q1,
one may do something like this :

...
with encap_msg( msg )      as aGeneratorINSTANCE: # tradeoffs of small-[TIME] add-on cost
     try:                                         #          for safe-[SPACE] savings
         for aMessageFRAME in aGeneratorINSTANCE: #
             socket.send_multipart( aMessageFRAME,# Hopes with us to see Zero-copy effect
                                    zmq.SNDMORE ) #
     except:                                      #
             pass                                 #
     finally:                                     # Dirty? Perhaps, but works safe
             socket.send_multipart( "",           #        one (empty) tail message
                                    zmq.DONTWAIT )# Not to remain hang waiting on API
     pass                                         #        hope you enjoy this
...

Upvotes: 1

Related Questions