From: Hermann H. <her...@we...> - 2015-06-17 08:11:48
|
<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div> <div> </div> <div>I want to suggest a small change to pyusb to improve speed.</div> <div>Currently you can only provide an array.array buffer to put the USB data into.<br/> I use the multiprocessing feature and the final destination of the USB data is a mmap.mmap<br/> Object. I would like to specify this as a receive buffer to save a pickle and<br/> unpickle operation.</div> <div>It is not so difficult to use the pyapi.PyObject_GetBuffer function to retrieve address and length<br/> of any buffer protocol compliant object.</div> <div>So this array restriction should be lifted, I assume there are more users who would<br/> appreciate this change. I would not like to distribute my software with a modified pyusb.</div> <div>Sincerely<br/> Hermann Hamann</div> <div><br/> Here follows the modifications I made:</div> <div>-----------------------------------------</div> <div># in backend1.py, similar for other backends</div> <div>from ctypes import *<br/> from struct import unpack</div> <div>getBuffer = pythonapi.PyObject_GetBuffer</div> <div>getBuffer.argtypes = [py_object, c_void_p, c_int]</div> <div>view = create_string_buffer(11*sizeof(int))</div> <div>def getBufferInfo(someObject):</div> <div> pointer_to_sO = py_object(someObject)</div> <div> result = getBuffer(pointer_to_sO,view,1) # flag writeable</div> <div> ### this call can raise an Exception. However this would be a consequence<br/> ### of an invalid caller, so no recovery is possible and simply let it<br/> ### crash, the message is helpful.<br/> <br/> if result != 0 : return None,None<br/> <br/> bufadr,objadr,buflen,itemsize,readonly,ndim,bformat,shape,strides,\<br/> suboffsets,internal = unpack("11I",view)<br/> <br/> result = pythonapi.PyBuffer_Release(string_buffer)</div> <div> return (bufadr,buflen)<br/> <br/> # and some lines later</div> <div> def __read(self, fn, dev_handle, ep, intf, buff, timeout):<br/> ### !!!! changed address, length = buff.buffer_info()<br/> address,length = getBufferInfo(buff)<br/> ##length *= buff.itemsize changed<br/> <br/> ----------------------------------<br/> # in core.py</div> <div> if isinstance(size_or_buffer,int):<br/> </div> </div></div></body></html> |