On Mon, 9 Feb 2009, Gabriel Genellina wrote: > En Mon, 09 Feb 2009 07:43:36 -0200, John O'Hagan <resea...@johnohagan.com> > > escribió: > > I'm using the socket module (python 2.5) like this (where 'options' > > refers to > > an optparse object) to connect to the Fluidsynth program: > > > > host = "localhost" > > port = 9800 > > fluid = socket(AF_INET, SOCK_STREAM) > > try: > > fluid.connect((host, port)) #Connect if fluidsynth is > > running > > except BaseException: > > print "Connecting to fluidsynth..." #Or start fluidsynth > > soundfont = options.soundfont > > driver = options.driver > > Popen(["fluidsynth", "-i", "-s", "-g", "0.5", > > "-C", "1", "-R", "1", "-l", "-a", driver, "-j", > > soundfont]) > > timeout = 50 > > while 1: > > timeout -= 1 > > if timeout == 0: > > print "Problem with fluidsynth: switching to > > synth." > > play_method = "synth" > > break > > try: > > fluid.connect((host, port)) > > except BaseException: > > sleep(0.05) > > continue > > else: > > break > > > > (I'm using BaseException because I haven't been able to discover what > > exception class[es] socket uses). > > Usually socket.error, which is a subclass of IOError, but others might > happen too I think. In any case, the most generic except clause you should > use is > try: > except Exception: ... > (because you usually don't want to catch KeyboardInterrupt nor SystemExit, > that is, let Ctrl-C and sys.exit() do their work)
Thanks, I had been doing "from socket import foo, bar" without importing "error", that now works. I think I should use that specific exception in the try clause for now so I can see any other exceptions that may occur, a good idea? > > > The problem is that this fails to connect ( the error is "111: Connection > > refused") the first time I run it after booting if fluidsynth is not > > already > > running, no matter how long the timeout is; after Ctrl-C'ing out of the > > program, all subsequent attempts succeed. Note that fluidsynth need not > > be > > running for a success to occur. > > Always the same exception? In both lines? Yes; I'm sure this is obvious, but just for clarity: if fluidsynth is not running I expect that error at the first line where connection is attempted, and at the second line during the loop while fluidsynth is loading (this takes 0.5-5 seconds depending on the size of the soundfont it's using). But I don't understand why the connection is still refused once fluidsynth has finished loading. I received an off-list reply to this post to the effect that attempting to reuse a socket on which a connection attempt has failed, will fail, and that I needed to create a new socket for each attempt. It seemed very plausible, so I tried inserting a fresh fluid = socket(AF_INET, SOCK_STREAM) before the second fluid.connect() in the loop; but this still behaved the same way. Is that the way to do it? Annoyingly, I've so far only been able reproduce the problem by rebooting. Regards, John -- http://mail.python.org/mailman/listinfo/python-list