improving performance of writing into a pipe

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Feb 18 10:21:53 EST 2013


On 18 February 2013 15:12,  <mikprog at gmail.com> wrote:
> Hi guys,
>
> on an embedded linux system (BeagleBoard) I am writing data coming from bluetooth dongle into a pipe.
> The function is the following one:
>
>
> def write_to_pipe(line):
>
>     # next line ensures that bytes like '0x09' are not translated into '\t' for
>     #example, and they are sent as such
>     hexbytes = "\\x" + "\\x".join([hex(ord(c))[2:].zfill(2) for c in line])
>     wrap = ["echo -en '", "' > /tmp/mypipe"]
>     msg = hexbytes.join(wrap)
>     print "DBG: sending: ", msg
>
>     try:
>         os.popen( msg )
>     except:
>         print "Error: write_to_pipe has failed!"
>
>
> Now I typically receive 4 bytes from the bluetooth dongle and that is fine.
> However when I receive many more than that it seems that the writing into the pipe is too slow.
>
> Is there any clever/obvious way to improve the code above?
> (I am quite sure there is to be honest).

Can you not open the pipe file directly in Python code? e.g.

fout = open('/tmp/mypipe', 'w')
fout.write(data)

I guess that this would be more efficient than using os.popen to run echo.


Oscar



More information about the Python-list mailing list