繁体   English   中英

从我的python程序运行外部程序

[英]Running external program from my python program

我有一个时钟GUI程序,我需要从中运行另一个python程序,但是我的时钟停止了,或者当我关闭客户端程序并且我需要父程序继续运行时,我的程序已经失效。 我试过的是:

os.system(run my program)  "This stops the parent clock"
os.popen(run my program)  "This stops the parent clock"
subprocess.call(run my program)  "This stops the parent clock"
subprocess.Popen(run my program)  "This works but when the client is closed goes defunct"

有没有一种方法可以运行我的外部程序,而无需停止我的时钟并且不退出已失效的进程?

您是否尝试过使用带有nohup的子流程模块?

这样的事情应该阻止Sigint退出子进程

import subprocess
import os

def run_process(self,cmd):
    subprocess.Popen("nohup " + cmd, shell=True, executable='/bin/bash', 
                     stdout=open('/dev/null', 'w'),
                     stderr=open('logfile.log', 'a'),
                     preexec_fn=os.setpgrp)

run_process('ls -R ~') # Or whatever you are trying to run

这对我有用:

导入信号

signal.signal(signal.SIGCHLD,signal.SIG_IGN)

这可行!

如果要同时运行不同的程序,则可以为每个子进程使用线程。

导入线程

t = threading.Thread(target = myprogram)t.start()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM