在GUI的容器中运行python脚本 [英] Run python script in a container in a GUI

查看:247
本文介绍了在GUI的容器中运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在GUI的容器中运行脚本的方法.

I am looking for a way to run scripts in a container in my GUI.

GUI生成了一个脚本,我想在不影响我的GUI的情况下运行该脚本. 我的问题是我的脚本中的所有导入和类在运行后仍保留在内存中,并在GUI中生成错误. 是否可以运行脚本,获取结果并删除运行scrpt的所有后果?

The GUI generate a script and i want to run this script without effect on my GUI. My problem is that all the imports and class in my script stay in memory after the run and generate errors in the GUI. Is it possible to run the script, get the result and delete all consequences of the scrpt run?

我尝试了多处理,线程化,但是它不起作用.我怎样才能做到这一点?非常感谢!

I tried with multiprocessing,threading but It is not working. How can I do that? Thanks a lot!

您好,MátyásKuti,我现在有多处理的情况.我想找到一种在某种容器中运行该脚本的方法,该脚本在脚本停止时可以删除.

Hi Mátyás Kuti, I just have the case with multiprocessing for now. I want to find a way to run it in a sort of container that I can delete when the script stop.

@pyqtSlot()
def run_func():
    run="""
    import os
    import sys
    from setup import *
    print('toto')
    print('titi')
    """
    from multiprocessing import Pool   
    pool = Pool(processes=4)          
    asyncResult = pool.apply_async(exec(run),{},{}),range(1)    

推荐答案

您可以尝试将脚本变成函数:

You could try making the script into a function:

script = """
def func():
""" + yourScript
yourGlobals = ...
exec script in yourGlobals
yourGlobals["func"]()

上面的代码只是一般性的想法:您将必须对其进行修复以使yourScript缩进.实际上,由脚本创建的所有全局变量都是func的局部变量,因此在func返回后将被删除.导入可能不会被删除,包表中将继续有一个条目.

The above code is just the general idea: you will have to fix it to indent yourScript. All globals created by your script will in fact be locals to func so will be removed after func returns. The imports will probably not be removed, there will continue to be an entry in the package table.

如果由于导入未清除而上述操作还不够,也许您可​​以描述该错误以及遗留哪些导入导致错误,这可能还有另一种解决方法.

If the above is not sufficient because imports don't get purged, perhaps you can describe the error and what imports are left that cause error, may be there is another way around this.

如果脚本需要很长时间才能运行,则上述技术也可以在单独的线程中工作.

If the script can take a long time to run then the above technique can work in a separate thread too.

这篇关于在GUI的容器中运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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