简体   繁体   English

Python脚本一直在后台运行

[英]Python script keeps running in the background

This is a simple script that is used to calculate Z scores of some neuropsychological tests. 这是一个简单的脚本,用于计算某些神经心理学测试的Z分数。

But recently the code seems to be running on the background even after exiting the program. 但是最近,即使退出程序,该代码似乎仍在后台运行。 This problem didn't exist before, I use the following startup piece to ensure program is running with elevation, and is correct size for the display. 以前不存在此问题,我使用以下启动程序来确保程序以高程运行,并且显示尺寸正确。

Program can be shut down using a "exit" command that raises SystemExit or just by pressing the X button on the top bar. 可以使用引发SystemExit的“退出”命令关闭程序,也可以仅按顶部栏上的X按钮来关闭程序。 Program keep running on the background regardless. 程序继续在后台运行。

Where did I go wrong? 我哪里做错了?

def progStructure():
    first_run = True
    if first_run:
        import os
        os.system("mode con: cols=160 lines=50")
        print("""
    ================================================
    PROGRAM GREETING
    ================================================
    """)

        mainStartup()
        first_run = False

    while settings("auto_run"):
        mainStartup()
        #this function contains the main program, but irrelevant to the question

    print("Auto shutdown enabled, program is shutting down.")

    wait(2)
    exit()
        #informs the user data has been saved then restarts


import ctypes, sys

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    progStructure()
else:
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, "", None, 0)
    progStructure()

My excel writing function: 我的Excel写作功能:

def excelWriter(excel_path, data_num, printable_list):
    from time import strftime
    date = strftime("%Y-%m-%d")
    time = strftime("%H:%M:%S")
    if settings("excel_output_subjectNames"):
        patient_name_local = patient_name      
    else:
        patient_name_local = "N/A"

    demographic_data = [patient_ID, patient_name_local, patient_admin, date, time, patient_age, patient_sex, patient_edu]

    from openpyxl import Workbook
    from openpyxl import load_workbook

    wb = Workbook(write_only=False)

    try:
        test_name_list = [
                    "(1)MMT", "(2)MOCA", "(3)3MS", "(4)GISD", "(5)ECR",
                    "(6)Sözel Bellek Süreçleri", "(7)Rey Karmaşık Figür", "(8)İz Sürme", "(9)Stroop",
                    "(10)Wisconsin", "(11)Görsel Sözel Test", "(12)Renkli İz Sürme",
                    "(13)Wechsler", "(14)Wechsler-Sayı Dizisi", "(15)Sözel Akıcılık", 
                    "(16)Semantik Akıcılık", "(17)Saat Çizme", "(18)SDOT", "(19)Ayları İleri-Geri Sayma"
                    ]


        while True:
            try:
                data_workbook = load_workbook(filename = excel_path + settings("excel_name"), read_only=False)
                active_sheet = data_workbook.get_sheet_by_name(test_name_list[data_num-1])
                active_sheet.append(demographic_data + printable_list)

                data_workbook.save(filename = excel_path + settings("excel_name"))
                break

            except:
                for i in range(len(test_name_list)):
                    wb.create_sheet(title = test_name_list[i])

                wb.save(filename = excel_path + settings("excel_name"))
                data_workbook = load_workbook(filename = excel_path + settings("excel_name"), read_only=False)
                active_sheet = data_workbook.get_sheet_by_name("Sheet")
                data_workbook.remove_sheet(active_sheet)
                data_workbook.save(filename = excel_path + settings("excel_name"))
                continue

    except:
        raise

写完之后忘了关闭excel文件,看似已解决了问题,感谢大家的帮助。

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

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