Low level I/O: because I could

Rob Gaddi rgaddi at highlandtechnology.invalid
Wed May 10 18:30:45 EDT 2017


Sorry, but I'm just too proud of this.

Given that you have:

   class RegisterLayout(ctypes.Structure):
     ...yadayadayada...

You can then:

   fh = os.open('/dev/devicethingy', os.O_RDWR)
   mm = mmap.mmap(fh, ctypes.sizeof(RegisterLayout))
   registers = RegisterLayout.from_buffer(mm)

And it just works.  Behaves exactly the same way memory-mapping that 
struct in C would.  Sure the accesses take dict lookups, and that 
definitely slows you down a bit.  If you REALLY really needed that speed 
you'd be writing C.  But it works.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list