Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python While循环在一段时间后挂起,按下时继续(回车)?_Python_Powershell_While Loop - Fatal编程技术网

Python While循环在一段时间后挂起,按下时继续(回车)?

Python While循环在一段时间后挂起,按下时继续(回车)?,python,powershell,while-loop,Python,Powershell,While Loop,我已经编写了一个python脚本并转换成python exe,它每秒打印应用程序名称和时间。 还添加了一个每30秒运行一次的计划功能 一个小时后,当我再次检查时,它运行得非常好,python脚本被暂停(或挂起)。当我按下[Enter]时,它像往常一样再次开始执行。剧本在一个小时内效果很好。 暂停的原因是什么 我的代码 from win32gui import GetForegroundWindow import psutil import time, os, dateti

我已经编写了一个python脚本并转换成python exe,它每秒打印应用程序名称和时间。 还添加了一个每30秒运行一次的计划功能

一个小时后,当我再次检查时,它运行得非常好,python脚本被暂停(或挂起)。当我按下[Enter]时,它像往常一样再次开始执行。剧本在一个小时内效果很好。 暂停的原因是什么

我的代码

    from win32gui import GetForegroundWindow
    import psutil
    import time, os, datetime, requests, json, schedule
    import win32process
    import csv
    import getpass

data = {}
process_time = {}
timestamp = {}

data['User'] = getpass.getuser()
data['Date'] = datetime.datetime.now().strftime("%d/%m/%Y")
data['Time'] = datetime.datetime.now().strftime("%I:%M %p")


def show_name():
    l = []
    for a, t in process_time.items():
        e = {}
        e['AppName'] = a
        e['Time'] = t
        l.append(e)
    data["Data"] = l
    
    data=json.dumps(data))
    print(data)



schedule.every(30).seconds.do(show_name)

while True:
    try:
        current_app = psutil.Process(win32process.GetWindowThreadProcessId(GetForegroundWindow())[1]).name().replace(".exe", "")
        timestamp[current_app] = int(time.time())
        time.sleep(1)
        if current_app not in process_time.keys():
            process_time[current_app] = 0
        process_time[current_app] = process_time[current_app]+int(time.time())-timestamp[current_app]

        print(process_time)

        schedule.run_pending()
        time.sleep(1)

        service_directory = os.path.abspath(os.path.dirname(__file__)) + '/test1.csv'
        with open(service_directory, 'w', encoding='UTF8', newline='') as f:
            for key in process_time.keys():
                f.write("%s,%s\n"%(key, process_time[key]))
    except:
        pass