Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:134': 0.05; 'except:': 0.07; 'msg': 0.07; 'skip:/ 10': 0.07; 'try:': 0.07; "'w')": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'slow.': 0.09; 'subject:into': 0.09; 'def': 0.10; 'above?': 0.16; 'fine.': 0.16; 'line)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:writing': 0.16; 'wrote:': 0.17; 'bytes': 0.17; 'skip:" 30': 0.20; 'pipe': 0.22; 'seems': 0.23; 'linux': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'embedded': 0.27; 'translated': 0.27; 'header:X-Complaints-To:1': 0.28; 'wrap': 0.29; "skip:' 10": 0.30; 'function': 0.30; 'code': 0.31; 'print': 0.32; 'to:addr:python- list': 0.33; 'typically': 0.33; 'there': 0.35; 'next': 0.35; 'received:org': 0.36; 'too': 0.36; 'quite': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'email addr:gmail.com': 0.63; 'more': 0.63; 'receive': 0.71; 'dongle': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Serhiy Storchaka Subject: Re: improving performance of writing into a pipe Date: Mon, 18 Feb 2013 21:29:09 +0200 References: <76620a9e-45fe-499d-b1bf-06b1d2a91c25@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 134.249.66.55 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 In-Reply-To: <76620a9e-45fe-499d-b1bf-06b1d2a91c25@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361215987 news.xs4all.nl 6974 [2001:888:2000:d::a6]:57713 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39119 On 18.02.13 17:12, mikprog@gmail.com wrote: > 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). def write_to_pipe(line): hexbytes = ''.join('\\x%02x' % ord(c) for c in line) with open('/tmp/mypipe', 'w') as f: f.write(hexbytes)