Small socket problem

Discussion in 'Python' started by John O'Hagan, Feb 9, 2009.

  1. John O'Hagan

    John O'Hagan Guest

    Hi,

    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).

    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.

    I've also tried it without the while loop, simply sleeping for a few seconds
    to give fluidsynth time to start (not a preferred approach as I want a short
    startup), but the same thing happens.

    I am a long way from being a networking guru and am at a loss as to how to
    debug this. Maybe someone can point out some flaw in my use of socket.

    Thanks,

    John O'Hagan
    John O'Hagan, Feb 9, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,770
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    950
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    532
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    649
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    704
Loading...

Share This Page