![](/img/trans.png)
[英]How can I cause a python script to run everytime I open a Google Sheet
[英]How can I open a cmd window everytime I run a python script?
现在我有两个 python 脚本“GUI.py”和“main.py”。 “GUI.py”是使用 pyQt5 编写的。 首先,我打开一个 cmd window 并运行“python GUI.py”。它会打开一个 window,其中包含一个名为“connect”的按钮。 每次单击按钮时,都会执行如下代码的 onclick() 回调 function:
def onclick():
subprocess.Popen("python main.py")
但是,日志仍然打印在我最初打开的 cmd window 中。这不是我想要的,因为很难分辨哪个日志属于哪个“main.py”。 有什么解决办法吗?
您有两种选择来阻止控制台中子进程的 output 第一个是使用 subprocess.PIPE 将数据从子进程发送到缓冲区并仅在子进程使用子进程的通信方法完成时读取 ut:
def onclick():
subprocess.Popen("python main.py", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
如果您根本不需要母进程中子进程的 output,则 go 与 subprocess.DEVNULL:
def onclick():
subprocess.Popen("python main.py", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
编辑:
如果您根本不想在主进程中拥有子进程的 output,或者 output 太大而 PIPE 实际上可能会崩溃。 您最好使用带有 DEVNULL 和日志记录模块的 go:
#In child process setup logging
logging.basicConfig(
filename="logfile", format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
#then in child process substitute all print to logging.info, logging.warning, #logging.error and whatewer you want.
# Then start child process the following way from master process:
def onclick():
subprocess.Popen("python main.py", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
希望回答对你有用,欢迎提问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.