简体   繁体   English

从另一个模块运行时,Python BackgroundScheduler程序崩溃

[英]Python BackgroundScheduler program crashing when ran from another module

I am trying to build a application that will run a bash script every 10 minutes. 我正在尝试构建一个将每10分钟运行一次bash脚本的应用程序。 I am using apscheduler to accomplish this and when i run my code from terminal it works like clock work. 我正在使用apscheduler完成此操作,当我从终端运行代码时,它的工作原理类似于时钟工作。 However when i try to run the code from another module it crashes i suspect that the calling module is waiting for the "schedule" module to finish and then crash when that never happens. 但是,当我尝试从另一个模块运行代码时,它崩溃了,我怀疑调用模块正在等待“时间表”模块完成,然后崩溃,而这永远不会发生。

Error code 错误代码

/bin/bash: line 1: 13613 Killed                  ( python ) < /tmp/vIZsEfp/26

shell returned 137   

Function that calls schedule 调用时间表的功能

   def shedual_toggled(self,widget):
            prosessSchedular.start_background_checker()

Schedule Program 计划程序

def schedul_check(): 
    """set up to call prosess checker every 10 mins"""
    print "%s check ran" %(counter) 
    counter =+ 1

    app = prosessCheckerv3.call_bash() < calls the bash file
    if app == False:
        print "error with bash" 
        return False 
    else:
        prosessCheckerv3.build_snap_shot(app)


def start_background_checker():
    scheduler = BackgroundScheduler()
    scheduler.add_job(schedul_check, 'interval', minutes=10)
    scheduler.start()

    while True:
        time.sleep(2)


if __name__ == '__main__':
    start_background_checker()   

this program simply calls another ever 10 mins. 该程序仅调用另一个10分钟。 As a side note i have been trying to stay as far away from multi-threading as possible but if that is required so be it. 顺带一提,我一直在尝试尽可能远离多线程,但是如果需要的话,就这样吧。

Well I managed to figure it out my self. 好吧,我设法弄清楚了自己。 The issue that GTK+ is not thread safe so the timed module need to be either be ran in another thread or else you can realise/enter the thread before/after calling the module. GTK +不是线程安全的问题,因此需要在另一个线程中运行定时模块,否则您可以在调用模块之前/之后实现/输入该线程。

I just did it like this. 我就是这样做的。

def shedual_toggeld(self,widget):
        onOffSwitch = widget.get_active()

        """ After main GTK has logicly finished all GUI work run thread on toggel button """
        thread = threading.Thread(target=self.call_schedual, args=(onOffSwitch,))
        thread.daemon = True
        thread.start()



    def call_schedual(self, onOffSwitch):
        if onOffSwitch == True:
            self.sch.start_background_checker()
        else:
            self.sch.stop_background_checker() 

This article goes through it in more detail. 本文将对其进行详细介绍。 Hopefully some one else will find this useful. 希望其他人会发现这很有用。 http://blogs.operationaldynamics.com/andrew/software/gnome-desktop/gtk-thread-awareness http://blogs.operationaldynamics.com/andrew/software/gnome-desktop/gtk-thread-awareness

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

相关问题 运行时 Python 中的程序错误(Python 2.7) - Program Error in Python When Ran (Python 2.7) 从终端运行并从Python运行时,脚本的工作方式不同 - Script works differently when ran from the terminal and ran from Python 从 python 中的脚本运行时,在 C 程序段错误中嵌入 Python - Embedded Python in C program seg faults when ran from script in python 如何停止/终止 python 脚本与 APScheduler 的 BackgroundScheduler() 一起运行 - How to stop/terminate a python script from running with BackgroundScheduler() of APScheduler Python作为脚本或模块运行了吗? - Python ran as a script or module? 当程序直接运行时,更改 python 程序中的墙纸有效。 但在从服务运行时不起作用 - changing the wallpaper in python program works when the program is ran directly. but doesn't work when it's run from a service 如何访问Python中子进程上运行的另一个程序的日志 - How to access logs of another program ran on subprocess in Python 从系统计划程序运行时,Python不存在 - Python is not existing when ran from System Scheduler 从python运行时wkhtmltopdf段错误 - wkhtmltopdf segfault when ran from python 有什么方法可以阻止python程序崩溃吗? - Is there any method to stop python program from crashing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM