简体   繁体   English

从另一个Python进程中打开另一个Python脚本

[英]Open another Python script from another Python process

I'm trying to open a python script from the main python program in a separate process. 我试图在一个单独的过程中从主python程序中打开python脚本。

For now let's just say that "main program" is a PyQt4 GUI program and "script" is the script (in a separate file) I am trying to run from my main program. 现在,我们只说“主程序”是PyQt4 GUI程序,“脚本”是我试图从主程序运行的脚本(在单独的文件中)。

Why? 为什么?

  1. So the script keeps running after the main program is closed 因此,脚本在主程序关闭后继续运行

  2. So that when the script is ran my main program doesn't freeze while waiting for the script with an infinite loop to end. 这样,当脚本运行时,在等待无限循环结束的脚本时,主程序不会冻结。

I know that subprocess.Popen() , subprocess.call() and os.system() can open the file via the command line, but when they open a script with an infinite loop the main program hangs and crashes. 我知道subprocess.Popen() subprocess.call()os.system()可以打开通过命令行的文件,但是当他们打开脚本具有无限循环主程序挂起和崩溃。

I also know that I could use QtCore.QCoreApplication.processEvents() to keep the main program running, but this does not work in my case. 我也知道我可以使用QtCore.QCoreApplication.processEvents()来保持主程序运行,但是在我的情况下不起作用。

So I figured the best solution to keep the script and the Main Program correctly running is the have separate processes. 因此,我认为保持脚本和主程序正确运行的最佳解决方案是使用单独的进程。

How would I open script.py file in a separate process or in a way that would not freeze up my program. 我将如何以单独的过程不会冻结程序的方式打开script.py文件。

Calling an external command in Python is probably what you're looking for. 您正在寻找在Python中调用外部命令 The author perfectly describes how to launch different python script that keeps running when your main program is closed. 作者完美地描述了如何启动不同的python脚本,这些脚本在您的主程序关闭时保持运行。

Don't run python scripts as subprocesses, import the corresponding modules and call the desired functions instead. 不要将python脚本作为子进程运行,不要导入相应的模块并调用所需的函数。 If you need to run Python code in a separate process, you could use multiprocessing : 如果需要在单独的进程中运行Python代码,则可以使用multiprocessing

multiprocessing.Process(target=infinite_loop, args=['arg 1', 2]).start()

Related: Call python script with input with in a python script using subprocess . 相关: 使用subprocess在带有python脚本的输入中调用python脚本

To avoid "freezing" your GUI, do not call functions that block for long in your GUI thread. 为避免“冻结” GUI,请勿在GUI线程中调用长时间阻塞的函数。 Either use threads or async. 使用线程或异步。 API (here's tkinter code example that uses createfilehandler() and .after() calls, to read output from a subprocess without blocking the GUI ). API(这是createfilehandler()代码示例,该示例使用createfilehandler()createfilehandler() .after()调用来读取子createfilehandler()输出而不会阻塞GUI )。

Popen() only starts a child process and it does not wait for it to exit. Popen()仅启动子进程,并且不等待其退出。 If Popen() "freezes" your program something else is broken. 如果Popen() “冻结”了您的程序,则其他程序已损坏。 Here's code example that starts/stops a tkinter progressbar on start/end of a subprocess. 是在子流程的开始/结束时启动/停止tkinter进度栏代码示例。

Qt has its own API: QThread, QProcess, signals that you could use to run a subprocess. Qt有自己的API:QThread,QProcess,可以用来运行子进程的信号。 Related: How to do stuff during and after a child process . 相关: 在子进程期间和之后如何做事

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

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