打开网络浏览器后如何恢复程序(或退出)? [英] How to resume program (or exit) after opening webbrowser?

查看:112
本文介绍了打开网络浏览器后如何恢复程序(或退出)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小的Python程序,该程序调用webbrowser模块来打开URL.打开URL效果很好.

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

该程序不能像执行print "Exiting..."那样有效,我之所以添加它是因为我注意到该程序由于某种原因没有离开if语句.

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.

如果很重要,我会从命令行运行该程序. 编辑:我正在通过反向端口使用KDE 4.3在Kubuntu 9.04 i386上运行.我使用Firefox 3.5作为我的默认浏览器,在KDE的系统设置中声明了它,并由程序正确调用了它. (至少,在Firefox中会打开一个新标签,其中包含所需的URL,我相信这是所需的功能.)/编辑

I am running this program from the command line, in case that's important. 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

此外,我认为几乎所有外部调用都将发生此问题,但是我对Python还是陌生的,并且不知道在该站点上搜索的术语. (搜索"python webbrowser"没有任何帮助.)因此,对于是否已经在其他标题下进行了讨论,我深表歉意!

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!

有什么建议吗?

推荐答案

在这里最简单的操作可能是分叉.我很确定这很不幸在Windows中不起作用,因为我认为它们的过程模型可能与类似Unix的操作系统不同.但是,该过程将类似.

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.

import os
import sys

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

这篇关于打开网络浏览器后如何恢复程序(或退出)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆