improving performance of writing into a pipe

Peter Otten __peter__ at web.de
Tue Feb 19 10:55:38 CET 2013


mikprog at gmail.com wrote:

> On Monday, February 18, 2013 6:12:01 PM UTC, Michael Torrie wrote:
>> On 02/18/2013 10:00 AM, mikprog at gmail.com wrote:
>> 
>> > [..]
>> 
>> >>
>> 
>> >> I don't see an exception in your answer. Where did you put it for us?
>> 
>> >>
>> 
>> > 
>> 
>> > well I just did print a message:
>> 
>> > 
>> 
>> >     PIPEPATH = ["/tmp/mypipe"]
>> 
>> > 
>> 
>> > [..]
>> 
>> >         try:
>> 
>> >             self.process = os.popen( self.PIPEPATH, 'w')
>> 
>> >         except:
>> 
>> >             print "Error while trying opening the pipe!"
>> 
>> >             print "check: ", self.PIPEPATH
>> 
>> >             exit()
>> 
>> > 
>> 
>> > I see the error messages.
>> 
>> 
>> 
>> Unfortunately your attempt to catch this exception is hiding the true
>> 
>> cause.  You need to give us the actual exception.  Otherwise it could be
>> 
>> anything from self.PIPEPATH not existing to who knows what.
>> 
>> 
>> 
>> Almost never do you want to catch all exceptions like you're doing.  You
>> 
>> should only catch the specific exceptions you know how to deal with in
>> 
>> your code.
>> 
>> 
>> 
>> For testing purposes, if your code really is as you put it, then
>> catching exceptions is kind of silly since you're just re-raising the
>> exception (sort of) but without any contextual information that would
>> make the error meaningful.
> 
> 
> Ok, I get your point.
> But on the other hand how do I know what to catch if I have no clue what
> is causing the error? There must be a way to catch all the possible errors
> and then investigate what is the problem, right? (which is not what I have
> done so far).
> 
> Or rather: what would you try to catch in this particular case?

Nothing. 

Once you get your script working you can try to provoke errors, and for 
those errors you can recover from you can write error handlers. For IOError 
and Python < 3.3 that may involve inspecting the errno attribute and 
conditionally reraising.

By the way, I don't think

>> >     PIPEPATH = ["/tmp/mypipe"]
>> >             self.process = os.popen( self.PIPEPATH, 'w')

can work. As a few people already told you the built-in open()

with open(PIPEPATH, "w") as f:
   f.write(...)

is the way to go.






More information about the Python-list mailing list