相关疑难解决方法(0)

从python运行程序,并在脚本被终止后继续运行

我试过这样的事情:

subprocess.Popen(['nohup', 'my_command'],
                 stdout=open('/dev/null', 'w'),
                 stderr=open('logfile.log', 'a'))
Run Code Online (Sandbox Code Playgroud)

如果父脚本正常退出,则此方法有效,但如果我终止脚本(Ctrl-C),则所有子进程也将被终止.有办法避免这种情况吗?

我关心的平台是OS X和Linux,使用的是Python 2.6 Python 2.7.

python subprocess nohup

37
推荐指数
3
解决办法
3万
查看次数

如何在Windows上捕获Python中的SIGINT?

(与此问题类似)

在Python 2.7下的UNIX上,在Python提示符下:

 >>> import signal
 >>> def handler(signal, frame):
 ...     print 'welcome to the handler'
 ...
 >>> signal.signal(signal.SIGINT, handler)
 <built-in function default_int_handler>
Run Code Online (Sandbox Code Playgroud)

我按ctrl-c

 >>> welcome to the handler

 >>>
Run Code Online (Sandbox Code Playgroud)

在Windows上:

 >>> import signal
 >>> def handler(signal, frame):
 ...     print 'welcome to the handler'
 ...
 >>> signal.signal(signal.SIGINT, handler)
 <built-in function default_int_handler>
Run Code Online (Sandbox Code Playgroud)

按ctrl-c:

 >>>
 KeyboardInterrupt
 >>>
Run Code Online (Sandbox Code Playgroud)

我可以验证handler正在安装Python端作为SIGINT的处理程序(调用signal.signal第二个计时器返回我的handler).如何在Windows上捕获SIGINT?

python windows signals

4
推荐指数
1
解决办法
4638
查看次数

标签 统计

python ×2

nohup ×1

signals ×1

subprocess ×1

windows ×1