Converting iris stream to python bytes is very slow
I'm trying to use embedded python code that receives an Iris %Stream.GlobalBinary and uses image manipulation library PIL.
Because PIL can't work with IRIS %Stream, I need to convert the image to python bytes.
This process seems to have very bad performance compared to writing to a file and then loading it from a file in python.
Is there a better way to send a stream into python? The conversion code I'm using is below.
Thanks.
def iris_stream_to_bytes(stream):
stream.Rewind()
s = ""
while not stream.AtEnd:
r = stream.Read(1024)
s += r
b = bytearray()
b.extend(map(ord, s))
return b
PythonPython
Product version: IRIS 2024.2
$ZV: IRIS for Windows (x86-64) 2024.2 (Build 247U) Tue Jul 16 2024 09:57:03 EDT
I'm using it all the time for string and i haven't seen any performance issue so far.
May be try to increase the buffer :
def stream_to_string(stream,buffer=1000000)-> str: string = "" stream.Rewind() while not stream.AtEnd: string += stream.Read(buffer) return string
Thanks, increasing the buffer to 1000000 reduced the processing of a 18MB image from 20 seconds to 2-3 seconds.
ClassMethod Stream2BytesArray(stream As %Stream.Object) As %SYS.Python
{
set builtin = $SYSTEM.Python.Builtins()
set b = builtin.bytearray()
do stream.Rewind()
while 'stream.AtEnd {
set r = $system.Python.Bytes(stream.Read($$$MaxLocalLength))
do b.extend(r)
}
quit b
}
Haven't had the chance to compare this method to converting it through python.
Do you think that doing the conversion on the cache side will be faster?
The numbers speak for themselves.