I think you are misunderstanding how the recv method works. You do not have
to know ahead of time exactly how many bytes are going to be returned to
you. You just need to tell it how many bytes to try and read. You may get
back less...

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 12121))
s.sendall("cmds.ls()")
ret = s.recv(4096)
print len(ret)
# 1305

Maya is not going to send you more than 4096 bytes for a response,
otherwise that will be an error instead. So you have the option of reading
the entire 4096 at once, or reading multiple times until you get back less
than what you asked for...

import cStringIO

SIZE = 1024
EOF = '\x00'

buf = cStringIO.StringIO()

while True:
    data = s.recv(SIZE)
    if not data or data == EOF:
        break
    buf.write(data)
    if len(data) < SIZE:
        break

print buf.getvalue()

But honestly, if the max buffer size of your commandPort is 4096, then its
probably not that bad to just read all 4096 bytes right away, since you are
most likely building a data structure out of the results anyways in memory.
Unless of course you are writing the bytes to a file and you set your
commandPort buffer to something huge for large return values.


On Thu, Mar 14, 2013 at 3:36 PM, vux <[email protected]> wrote:

> For example i have 2 maya functions:
> 1. This return simple string "Hello World"
> 2. This return list of millions face indices
> With socket.recv how to know what size to read from result for best
> performance. I dont need to read millions of bytes to read result from
> function 1 in agressive looped scripts. I need fast solution.
>
> And also i want to ask you. Did you tried communicate with maya with rpc.
> To import maya modules into other python, like houdini does it with hrpyc.
> I want to find best solution to send cmds from other pythons
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to