views:

197

answers:

3

I'm making a small Python program, which calls the webbrowser module to open a URL. Opening the URL works wonderfully.

My problem is that once this line of code is reached, the problem is unresponsive. How do I get the program to proceed past this line of code and continue to execute? Below the problematic line is the problematic line, in context:

if viewinbrowser == "y":
    print "I can definitely do that. Loading URL now!"
    webbrowser.open_new(url)
    print "Exiting..."
    sys.exit()

The program does not get as far as executing the print "Exiting...", which I added because I noticed the program wasn't leaving the if statement for some reason.

I am running this program from the command line, in case that's important. Edit: I am running on Kubuntu 9.04 i386, using KDE 4.3 via backports. I use Firefox 3.5 as my default browser, declared in the System Settings for KDE, and it is called correctly by the program. (At least, a new tab opens up in Firefox with the desired URL—I believe that is the desired functionality.) /Edit

Also, I assume this problem would happen with pretty much any external call, but I'm very new to Python and don't know the terminology to search for on this site. (Searching for "python webbrowser" didn't yield anything helpful.) So, I apologize if it's already been discussed under a different heading!

Any suggestions?

A: 

The webbrowser module makes a system call to start a separate program (the web browser), then waits ( "blocks" ) for an exit code. This happens any time you start a program from another program. You have to (A) write your own function that does not block waiting for the webbrowser to exit (by using threads, fork(), or similar), or find out if the webbrowser module has a non-blocking call.

Edward Amsden
+2  A: 

The easiest thing to do here is probably to fork. I'm pretty sure this doesn't work in windows unfortunately, since I think their process model might be different from unix-like operating systems. The process will be similar, though.

pid = os.fork()
if pid:
    # we are the parent, continue on
    print "This runs in a separate process from the else clause."

else:
    #child runs browser then quits.
    webbrowser.open_new(url)
    print "Exiting..."
    sys.exit()
McPherrinM
+3  A: 

This looks like it depends on which platform you're running on.

  • MacOSX - returns True immediately and opens up browser window. Presumably your desired behavior.
  • Linux (no X) - Open up links textmode browser. Once this is closed, returns True.
  • Linux (with X) - Opens up Konquerer (in my case). Returns True immediately. Your desired behavior.

I'm guessing you're on Windows, which, as another commentor mentioned doesn't have fork. I'm also guessing that the webbrowser module uses fork internally, which is why it's not working for you on Windows. If so, then using the threading module to create a new thread that opens the webbrowser might be the easiest solution:

>>> import webbrowser
>>> import threading
>>> x=lambda: webbrowser.open_new('http://scompt.com')
>>> t=threading.Thread(target=x)
>>> t.start()
scompt.com